简体   繁体   中英

Reduce image size without changing resolution android studio

In my android app, I use the Camera intent to click a photo and upload the same to my server for processing. However, there is a requirement that the resolution of the image should be more than 500X500.

The default camera resolution is much higher these days and hence the desired resolution is not an issue. However, in most cases the resolution is much higher and the image size comes up to 3 MB or so. Because of this, it takes longer to upload the image to server, especially in areas where internet connectivity is poor.

I would like to reduce the size of the image, preferably down to 100 KB or so, without reducing the resolution.

My code for capturing the image through intent is:

     imageHolder = (ImageView)findViewById(R.id.cPhoto78);
     imageHolder.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            File outputFile1 = new File(getExternalStorageDirectory(), "/APDCLSBM/APDCL1.jpg");
            outputFile1.delete();// Deletes the existing photo
            File photoFile = null;
            try {
                photoFile = createImageFile();

            } catch (IOException ex) {
                Toast.makeText(getBaseContext(),"Error in clicking picture",
                        Toast.LENGTH_SHORT).show();
                finish();
            }
           Intent a = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            if (photoFile != null) {
                Uri outputFileUri = FileProvider.getUriForFile(photoCamera.this,
                        BuildConfig.APPLICATION_ID + ".provider",
                        photoFile);
                a.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
                startActivityForResult(a, 1);
            }

        }
    });

The following function creates the image file and path:

    private File createImageFile() throws IOException {
    String imageFileName = "APDCL1";
    File storageDir = new File(
            getExternalStorageDirectory() + IMAGE_DIRECTORY);
    File image = new File(storageDir, imageFileName+".jpg");

    currentPhotoPath = image.getAbsolutePath();
    Log.d("path1",currentPhotoPath);
    return image;
}

Please suggest a way

private File createImageFile() throws IOException {
String imageFileName = "APDCL1";
File storageDir = new File(
        getExternalStorageDirectory() + IMAGE_DIRECTORY);
File image = new File(storageDir, imageFileName+".jpg");
try{
  Bitmap bitmap = BitmapFactory.decodeFile(image.getAbsolutePath);
}catch(Exception e){ e.printStacktrace(); return null;}

   File reducedSizeImage = new File.createTempFile("name",".jpg",location);
 try ( FileOutputStream outputStream = new FileOutputStream(reducedSizeImage)) 
 
      {

        image.compress(Bitmap.CompressFormat.JPEG,20, outputStream); // this line will reduce the size , try changing the second argument to adjust to correct size , it ranges 0-100

 
     } catch (Exception e) {
         e.printStackTrace();
         return null;
     }

   return reducedSizeImage // otherwise upload this file
}

Have a good day

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM