上传服务器上的加密图像

时间:2017-09-22 05:16:02

标签: php android image-uploading

我使用此https://www.youtube.com/watch?v=EmOzhbGFEAk&t=1118s上传服务器上的加密图像,按照本教程我可以加密图片,但无法上传..

当我调试它时,我发现在ImageUpload函数中,在postData中Image的大小为零,但不知道如何解决这个问题

这是我的MainActivity Class

public class MainActivity extends AppCompatActivity {

EditText editName, editfatherName, editCNIC, editReview, editRegistration;
Button btn_add, btn_retrieve;
ImageView takePhoto, openGallery, UploadImage, ivImage;
CameraPhoto cameraPhoto;
GalleryPhoto galleryPhoto;
final int CAMERA_REQUEST = 13323;
final int GALLERY_REQUEST=22131;
String SelectedPhoto;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    cameraPhoto=new CameraPhoto(getApplicationContext());
    galleryPhoto=new GalleryPhoto(getApplicationContext());
    takePhoto=(ImageView)findViewById(R.id.Takephoto);
    openGallery=(ImageView)findViewById(R.id.OpenGallery);
    UploadImage=(ImageView)findViewById(R.id.Image_upload);
    ivImage=(ImageView)findViewById(R.id.image);
    takePhoto.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            try {
                startActivityForResult(cameraPhoto.takePhotoIntent(),CAMERA_REQUEST);
            } catch (IOException e) {
                Toast.makeText(MainActivity.this, "Something wrong while taking photos", Toast.LENGTH_SHORT).show();
            }

        }
    });
    openGallery.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            startActivityForResult(galleryPhoto.openGalleryIntent(),GALLERY_REQUEST);


        }
    });
    UploadImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            try {
                Bitmap bitmap= ImageLoader.init().from(SelectedPhoto).requestSize(1024,1024).getBitmap();

//Change
//Store encrypt string
String encryptString=BitMapToString(bitmap);
//whenever you want to use image
Bitmap image=StringToBitmap(encryptString);
//

                String encodedImage = ImageBase64.encode(bitmap);
                Log.d("UploadImaes",encodedImage);
                HashMap<String,String> postData = new HashMap<String, String>();
                postData.put("image",encodedImage); // here the size of Image is zero

                PostResponseAsyncTask task = new PostResponseAsyncTask(MainActivity.this, postData, new AsyncResponse() {
                    @Override
                    public void processFinish(String s) {
                         if(s.contains("uploaded_success"))
                        {
                            Toast.makeText(MainActivity.this, "Image Upload Successfully", Toast.LENGTH_SHORT).show();
                        }
                        else
                        {
                            Toast.makeText(MainActivity.this, "Image Upload Failed", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                task.execute("http://localhost/news/upload.php");
                task.setEachExceptionsHandler(new EachExceptionsHandler() {
                    @Override
                    public void handleIOException(IOException e) {
                        Toast.makeText(MainActivity.this, "Cannot Connect to Server 1", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void handleMalformedURLException(MalformedURLException e) {
                        Toast.makeText(MainActivity.this, "Cannot Connect to Server 2", Toast.LENGTH_SHORT).show();

                    }

                    @Override
                    public void handleProtocolException(ProtocolException e) {
                        Toast.makeText(MainActivity.this, "Cannot Connect to Server 3", Toast.LENGTH_SHORT).show();

                    }

                    @Override
                    public void handleUnsupportedEncodingException(UnsupportedEncodingException e) {
                        Toast.makeText(MainActivity.this, "Cannot Connect to Server 4", Toast.LENGTH_SHORT).show();

                    }
                });

            } catch (FileNotFoundException e) {
                Toast.makeText(MainActivity.this, "Something wrong while encoding photos", Toast.LENGTH_SHORT).show();

            }

        }

    }); 
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      if(resultCode==RESULT_OK)
      {
          if(requestCode==CAMERA_REQUEST)
          {
             String photoPath =cameraPhoto.getPhotoPath();
            SelectedPhoto=photoPath;
              try {
                  Bitmap bitmap= ImageLoader.init().from(photoPath).requestSize(1024,1024).getBitmap();
                  ivImage.setImageBitmap(bitmap);
              } catch (FileNotFoundException e) {
                  Toast.makeText(MainActivity.this, "Something wrong while loading photos", Toast.LENGTH_SHORT).show();
              }
          }
          else  if(requestCode==GALLERY_REQUEST)
          {
              Uri uri=data.getData();
              galleryPhoto.setPhotoUri(uri);
              String photoPath=galleryPhoto.getPath();
              SelectedPhoto=photoPath;
              try {
                  Bitmap bitmap= ImageLoader.init().from(photoPath).requestSize(1024,1024).getBitmap();
                  ivImage.setImageBitmap(bitmap);
              } catch (FileNotFoundException e) {
                  Toast.makeText(MainActivity.this, "Something wrong while choosing photos", Toast.LENGTH_SHORT).show();
              }

          }
      }
}

//change
Copy and paste two function here.
//

这是我的Php脚本

 <?PHP
   if(isset($_POST['image']))
   {
$now = DateTime::createFromFormat('U.u', microtime(true));
$id = $now->format('YmdHisu');
$upload_folder = “upload"; 
$path = "$upload_folder/$id.jpeg";
$image = $_POST['image'];
if(file_put_contents($path, base64_decode($image)) !=false)
{
echo “uploaded_success";
    exit;}
else
{
    echo “uploaded_failed";
}
}
    else {
echo "image not in";
exit;
   }  
   ?>

1 个答案:

答案 0 :(得分:0)

将转换图像转换为位图而不是转换为Base64字符串而不是使用以下函数

public String BitMapToString(Bitmap bitmap) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
        byte[] b = baos.toByteArray();
        String temp = Base64.encodeToString(b, Base64.NO_WRAP);
        return temp;
    }

将转换后的位图转换为 Base64字符串图像,然后将字符串转换为位图,使用下面的函数

public Bitmap StringToBitmap(String encodedstring) {
        Bitmap decodedByte;
        try {
            byte[] decodedString = Base64.decode(encodedstring, Base64.DEFAULT);
            decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);

        } catch (Exception e) {
            decodedByte = null;
        }
        return decodedByte;
    }