0

My code..

mImageView_Photos.setImageBitmap(BitmapFactory.decodeFile(picturePath));

My logcat..

11-27 10:51:56.325: E/AndroidRuntime(3879): FATAL EXCEPTION: main
11-27 10:51:56.325: E/AndroidRuntime(3879): Process: com.virtualmaze.golfelite, PID: 3879
11-27 10:51:56.325: E/AndroidRuntime(3879): java.lang.OutOfMemoryError
11-27 10:51:56.325: E/AndroidRuntime(3879):     at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
11-27 10:51:56.325: E/AndroidRuntime(3879):     at android.graphics.BitmapFactory.decodeStreamInternal(BitmapFactory.java:620)
11-27 10:51:56.325: E/AndroidRuntime(3879):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:596)
11-27 10:51:56.325: E/AndroidRuntime(3879):     at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:376)
11-27 10:51:56.325: E/AndroidRuntime(3879):     at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:402)
11-27 10:51:56.325: E/AndroidRuntime(3879):     at com.virtualmaze.golfelite.HoleMapActivity.createImageView(HoleMapActivity.java:3839)
11-27 10:51:56.325: E/AndroidRuntime(3879):     at com.virtualmaze.golfelite.HoleMapActivity.onActivityResult(HoleMapActivity.java:3762)
11-27 10:51:56.325: E/AndroidRuntime(3879):     at android.app.Activity.dispatchActivityResult(Activity.java:5423)
11-27 10:51:56.325: E/AndroidRuntime(3879):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3347)
11-27 10:51:56.325: E/AndroidRuntime(3879):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3394)
11-27 10:51:56.325: E/AndroidRuntime(3879):     at android.app.ActivityThread.access$1300(ActivityThread.java:135)
11-27 10:51:56.325: E/AndroidRuntime(3879):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
11-27 10:51:56.325: E/AndroidRuntime(3879):     at android.os.Handler.dispatchMessage(Handler.java:102)
11-27 10:51:56.325: E/AndroidRuntime(3879):     at android.os.Looper.loop(Looper.java:136)
11-27 10:51:56.325: E/AndroidRuntime(3879):     at android.app.ActivityThread.main(ActivityThread.java:5001)
11-27 10:51:56.325: E/AndroidRuntime(3879):     at java.lang.reflect.Method.invokeNative(Native Method)
11-27 10:51:56.325: E/AndroidRuntime(3879):     at java.lang.reflect.Method.invoke(Method.java:515)
11-27 10:51:56.325: E/AndroidRuntime(3879):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
11-27 10:51:56.325: E/AndroidRuntime(3879):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
11-27 10:51:56.325: E/AndroidRuntime(3879):     at dalvik.system.NativeStart.main(Native Method)

How can I rectify this?

royhowie
  • 11,075
  • 14
  • 50
  • 67
S.Saranya
  • 25
  • 8

4 Answers4

5

When image size is big it shows java.lang.OutOfMemoryError

try this solutions ,

1) You can use Android-Universal-Image-Loader Library to set images efficiently, this is very nice solution

OR

2) You can shrink bitmap size before use it...

BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
bmpFactoryOptions.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);

int heightRatio = (int) Math.ceil(bmpFactoryOptions.outHeight / (float) height);
int widthRatio = (int) Math.ceil(bmpFactoryOptions.outWidth / (float) width);

if(heightRatio > 1 || widthRatio > 1)
{
    if(heightRatio > widthRatio)
    {
        bmpFactoryOptions.inSampleSize = heightRatio;
    }
    else
    {
        bmpFactoryOptions.inSampleSize = widthRatio;
    }
}

bmpFactoryOptions.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);
kiran boghra
  • 3,662
  • 2
  • 18
  • 23
0

try android:largeHeap="true" in application-tag in AndroidManifest.xml or, i think, you have to use NDK to manipulate with large images.

Vyacheslav
  • 26,359
  • 19
  • 112
  • 194
0

The way you are decoding the image/resource is not effecient.

you have to always scale the bitmap (appropriate to your image view size) and then decode and display, If needed do this in a background thread like an async task (with a loading symbol for better UX). It is in-effecient to decode a 20MP image to a thumbnail of size 50X50. It will simply waste memory.

Android has a good documentation on how to decode bitmaps properly. Check - http://developer.android.com/training/displaying-bitmaps/index.html

Other than that always put a try catch block in any api that can fail runtime, and handle it properly ,simplest you could do is displaying an error message to the user, instead of crashing ,it will notify the user something is wrong (better UX)

Hope this helps

Regards, Shrish

Shrish
  • 739
  • 5
  • 15
0

decode your file with lower resolution using BitmapFactory.Options

decodeFile(String pathName, BitmapFactory.Options opts)
Aun
  • 1,883
  • 1
  • 17
  • 26