My app sometimes crashes with an outOfMemoryError. I'm trying to handle it gracefully rather than crash the app. Here is the Logcat of the error, and then my try/catch block:
EDIT This question is about exception handling, not displaying images efficiently. I am sampling my images to an efficient size upon loading. However on an old phone it still may crash, as the user is able to add images manually to the activity. I want a message to come up saying they have added their limit instead of a crash. Thanks.
02-14 09:46:11.833: E/dalvikvm-heap(8495): Out of memory on a 2424016-byte allocation.
02-14 09:46:11.833: I/dalvikvm(8495): "main" prio=5 tid=1 RUNNABLE
02-14 09:46:11.833: I/dalvikvm(8495): | group="main" sCount=0 dsCount=0 obj=0x40fc3508 self=0x40fb2ff8
02-14 09:46:11.833: I/dalvikvm(8495): | sysTid=8495 nice=0 sched=0/0 cgrp=apps handle=1074818864
02-14 09:46:11.838: I/dalvikvm(8495): | schedstat=( 10854840575 3262089934 15014 ) utm=971 stm=113 core=1
02-14 09:46:11.838: I/dalvikvm(8495): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
02-14 09:46:11.838: I/dalvikvm(8495): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:625)
02-14 09:46:11.838: I/dalvikvm(8495): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:478)
02-14 09:46:11.838: I/dalvikvm(8495): at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:501)
02-14 09:46:11.838: I/dalvikvm(8495): at com.btf271.imagehelper.ImageHelper.decodeSampledBitmapFromResource(ImageHelper.java:218)
02-14 09:46:11.838: I/dalvikvm(8495): at com.btf271.multitouchimages.MultitouchImagesView$Img.load(MultitouchImagesView.java:285)
02-14 09:46:11.838: I/dalvikvm(8495): at com.btf271.multitouchimages.MultitouchImagesView.loadImages(MultitouchImagesView.java:134)
02-14 09:46:11.838: I/dalvikvm(8495): at com.btf271.fashionassistant.DressingRoomActivity.onResume(DressingRoomActivity.java:125)
02-14 09:46:11.843: I/dalvikvm(8495): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1199)
02-14 09:46:11.843: I/dalvikvm(8495): at android.app.Activity.performResume(Activity.java:5280)
02-14 09:46:11.843: I/dalvikvm(8495): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2629)
02-14 09:46:11.843: I/dalvikvm(8495): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2667)
02-14 09:46:11.843: I/dalvikvm(8495): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1279)
02-14 09:46:11.843: I/dalvikvm(8495): at android.os.Handler.dispatchMessage(Handler.java:99)
02-14 09:46:11.843: I/dalvikvm(8495): at android.os.Looper.loop(Looper.java:137)
02-14 09:46:11.843: I/dalvikvm(8495): at android.app.ActivityThread.main(ActivityThread.java:4921)
02-14 09:46:11.843: I/dalvikvm(8495): at java.lang.reflect.Method.invokeNative(Native Method)
02-14 09:46:11.843: I/dalvikvm(8495): at java.lang.reflect.Method.invoke(Method.java:511)
02-14 09:46:11.843: I/dalvikvm(8495): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
02-14 09:46:11.843: I/dalvikvm(8495): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
02-14 09:46:11.843: I/dalvikvm(8495): at dalvik.system.NativeStart.main(Native Method)
02-14 09:46:11.843: A/libc(8495): Fatal signal 11 (SIGSEGV) at 0x0000bed8 (code=1), thread 8495 (ashionassistant)
02-14 09:46:22.023: I/ActivityThread(8849): Pub com.btf271.ImageHelper.contentproviders.media: com.btf271.imagehelper.MediaContentProvider
Method causing the error: MultitouchImagesView.loadImages(MultitouchImagesView.java:134):
/** Called by activity's onResume() method to load the images */
public void loadImages(Context context) {
Resources res = context.getResources();
int n = mImages.size();
for (int i = 0; i < n; i++){
try{
mImages.get(i).load(res);
}
catch(OutOfMemoryError oom){
Log.d("out of memory", "out of memory");
}
catch( Exception ex) {
Log.d("general exception", ex.getMessage());
}
}
}
NKN's answer explains why it crashes well. So is there a solution?
More information about the app. The user adds one image at a time to an activity, for as many images as they want. So I will cap it off at the 6th image to avoid outOfMemory exceptions. But can I check for outOfMemory exceptions each time they add an image to the activity, just to be fail-safe, in case one phone cannot handle 6 images? (Some phones can handle 30 images, but some might only handle around 6).