【发布时间】:2014-03-12 23:29:44
【问题描述】:
我的应用有时会因 outOfMemoryError 而崩溃。我试图优雅地处理它而不是让应用程序崩溃。这是错误的 Logcat,然后是我的 try/catch 块:
EDIT 这个问题是关于异常处理的,不是有效地显示图像。我在加载时将图像采样到有效尺寸。但是在旧手机上它仍然可能崩溃,因为用户可以手动将图像添加到活动中。我想要一条消息说他们已经增加了他们的限制而不是崩溃。谢谢。
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
导致错误的方法: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 的回答解释了为什么它崩溃得很好。那么有解决办法吗?
有关该应用的更多信息。用户一次将一张图像添加到活动中,他们想要的图像数量不限。因此,我将在第 6 幅图像上加盖以避免 outOfMemory 异常。但是,我可以在每次将图像添加到活动时检查 outOfMemory 异常,以防万一,以防一部手机无法处理 6 张图像? (有些手机可以处理 30 张图片,但有些可能只能处理 6 张左右)。
【问题讨论】:
标签: android exception-handling