我的应用程序崩溃,在logcat中显示:
java.lang.OutOfMemoryError: (Heap Size=39047KB, Allocated=19932KB)
at android.graphics.BitmapFactory.nativeDecodeFile(Native Method)
at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:373)
at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:443)
at com.mApp.mobileapp.mActivity.onActivityResult(mActivity.java:196)
at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:153)
at android.app.Activity.dispatchActivityResult(Activity.java:4752)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3449)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3503)
at android.app.ActivityThread.access$1100(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1320)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:5109)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:991)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:758)
at dalvik.system.NativeStart.main(Native Method)
运行此代码时:
String selectedImagePath = data.getStringExtra("imageByteCode");
try {
File imageFile = new File(selectedImagePath);
Bitmap bitmap = BitmapFactory.decodeFile(imageFile
.getAbsolutePath());//line 196
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
base64code = selectedImagePath;
mImage.setImageBitmap(bitmap);
} catch (Exception e) {
}
由于我正在捕获它,所以我从没想到它会导致应用程序崩溃。
selectedImagePath
是SD卡上所选图像的路径,该路径不得超过 3m.b ,但堆大小= 39047KB,已分配= 19932KB ?谢谢你
最佳答案
您没有捕获它。 OutOfMemoryError
是从Error
派生的,而不是从Exception
派生的。这是一个Error
,因为通常不应该由应用程序捕获它,您的应用程序几乎无法做任何事情来恢复。
关于android - java.lang.OutOfMemoryError即使在try-catch块中?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14812508/