In android, I open a bitmap from the image picker, and load it into a imageview. If the user selects a big image, the app will crash. I tried try/catch, but it didn't work.
Is there a way to check the file size before loading it into a bitmap?
This is the code:
This is the return function from when I choose an image
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
ImageUploadHandler.handleResult(this, data);
}
}
this is from another file
public void handleResult(Context context, Intent data) {
Bitmap bitmap = null;
try {
bitmap = MyImage.GetBitmapFromPath(context, data.getData());
ByteArrayOutputStream out = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 100, out);
int size = out.size();
isReady = size <= IMAGE_THRESHOLD;
} catch (Exception e) {
isReady = false;
Log.d("Image Error", e.getMessage());
}
if (isReady) {
DialogImageView.setImageBitmap(bitmap);
DialogStatus.setText(context.getString(R.string.image_ok));
} else {
DialogImageView.setImageDrawable(null);
DialogStatus.setText(context.getString(R.string.too_big_image));
}
}
another file
public static Bitmap GetBitmapFromPath(Context context, Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = context.getContentResolver().query(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String filePath = cursor.getString(column_index);
cursor.close();
Bitmap bitmap = BitmapFactory.decodeFile(filePath);
return bitmap;
}
This line
bitmap = MyImage.GetBitmapFromPath(context, data.getData());
from the handleResult function, causes a outofmemory error when a user loads a big image.
How can I fix this?
Thanks.