I'm attempting to load an image from the Gallery that is big (around 30MB-70MB). The issue I keep facing is:
FATAL EXCEPTION: main Process: com.parse.starter, PID: 29795
java.lang.OutOfMemoryError: Failed to allocate a 1866240012 byte allocation with 2323392 free bytes and 380MB until OOM at dalvik.system.VMRuntime.newNonMovableArray(Native Method) at android.graphics.BitmapFactory.nativeDecodeStream(Native Method) at android.graphics.BitmapFactory.decodeStreamInternal(BitmapFactory.java:635) at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:611) at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:649) at android.provider.MediaStore$Images$Media.getBitmap(MediaStore.java:867) at com.parse.starter.AddPhotosActivity.onActivityResult(AddPhotosActivity.java:151) at android.app.Activity.dispatchActivityResult(Activity.java:6428) at android.app.ActivityThread.deliverResults(ActivityThread.java:3695) at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742) at android.app.ActivityThread.-wrap16(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Here is my code (line 151 as stated above in crash log) has a comment right above it. Basically, the end goal is I want to detect right off the bat whether or not the selected image is greater than 10MB. If so, then send a Toast, if not, then proceed with saving out the image to the backend.
if (requestCode == SELECT_PHOTO && resultCode == RESULT_OK)
{
Uri uri = data.getData();
File f = new File(uri.getPath());
try
{
// LINE BELOW IS THE LINE 151, ERROR CRASH
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
if (bitmap.getByteCount() > 10485760)
{
Toast.makeText(AddPhotosActivity.this, "That file is too large to attach to a Grievance, please try again!", Toast.LENGTH_SHORT).show();
}
else
{
mImageOne.setImageBitmap(bitmap);
SaveImageOne(bitmap);
}
}
catch (IOException e)
{
e.printStackTrace();
}
}