Java – How do I limit the size of the selected image

How do I limit the size of the selected image… here is a solution to the problem.

How do I limit the size of the selected image

My problem is consuming memory and I need to limit the size of the selected image.

Edit

I

don’t need to resize the image after loading, I can use

Bitmap.createScaledBitmap(image, (int) 80, (int) 80, true);

I need to prevent users from selecting image > 5 MB

My code

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
      if (requestCode == SELECT_PICTURE) {
        Uri selectedImageUri = data.getData();

} catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
             TODO Auto-generated catch block
            e.printStackTrace();
        }

}
    }
}

Limiting the folder size is not a better option.

Solution

I solved my problem with this method :

public boolean MaxSizeImage(String imagePath) {
    boolean temp = false;
    File file = new File(imagePath);
    long length = file.length();

if (length < 1500000) // 1.5 mb
        temp = true;

return temp;
}

If you need an imagePath, you can use this method

 public String getImagePath(Uri uri){
     Cursor cursor = getContentResolver().query(uri, null, null, null, null);
     cursor.moveToFirst();
     String document_id = cursor.getString(0);
     document_id = document_id.substring(document_id.lastIndexOf(":")+1);
     cursor.close();

cursor = getContentResolver().query(
             android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
             null, MediaStore.Images.Media._ID + " = ? ", new String[]{document_id}, null);
     cursor.moveToFirst();
     String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
     cursor.close();

return path;
 }

Solution

Try this Hackro 😀

First of all:

public static final int PICK_IMAGE = 1;
private void takePictureFromGalleryOrAnyOtherFolder() 
{
 Intent intent = new Intent();
 intent.setType("image/*");
 intent.setAction(Intent.ACTION_GET_CONTENT);
 startActivityForResult(Intent.createChooser(intent, "Select Picture"),PICK_IMAGE);
}

And then:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
           if (requestCode == PICK_IMAGE) {
                Uri selectedImageUri = data.getData();
                String imagePath = getRealPathFromURI(selectedImageUri);
               Now you have imagePath do whatever you want to do now
             }//end of inner if
         }//end of outer if
  }

public String getRealPathFromURI(String contentURI) {
    Uri contentUri = Uri.parse(contentURI);

String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = null;
    try {
        if (Build.VERSION.SDK_INT > 19) {
             Will return "image:x*"
            String wholeID = DocumentsContract.getDocumentId(contentUri);
             Split at colon, use second item in the array
            String id = wholeID.split(":")[1];
             where id is equal to
            String sel = MediaStore.Images.Media._ID + "=?";

cursor = context.getContentResolver().query(
                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                    projection, sel, new String[] { id }, null);
        } else {
            cursor = context.getContentResolver().query(contentUri,
                    projection, null, null, null);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

String path = null;
    try {
        int column_index = cursor
                .getColumnIndex(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        path = cursor.getString(column_index).toString();
        cursor.close();
    } catch (NullPointerException e) {
        e.printStackTrace();
    }
    return path;
}

Related Problems and Solutions