内存不足错误而载入位图位图、错误、内存不足

2023-09-12 10:17:09 作者:终遇

我有3 acitivtys一个Android应用程序:

i have an android app with 3 acitivtys:

A1 - 的启动的 - > A2 - 的启动的 - > A3 - 的当完成了这一过程:启动的 - > A1

A1 --starts--> A2 --starts--> A3 --when finished his process: starts--> A1

(所以我不完成();一个应用程序,我开始下activitys与startActivity(..);userinteraction后的全部时间)

(so i don't "finish();" an app. i start the next activitys with "startActivity(..);" the whole time after userinteraction)

所以在这些3 activitys一个循环。 在每一个活动,我显示3-9的照片,位于SD卡,这是我装入我的以下函数:

so there is a loop in these 3 activitys. On each Activity, i display 3-9 pictures, located on the SD-card, which i load with my following function:

try
{
    Uri selectedImageURI = Uri.parse(strImagePath);
    File imgFile = new  File(getRealPathFromURI(selectedImageURI, c));
    Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
    ivTmp.setImageBitmap(myBitmap);
}catch (Exception e)
{
    return null;
}

这所有的作品。 但有时(通过我的activitys循环几次后),我的应用程序崩溃。

This all works. But sometimes (after looping a few times through my activitys), my app crashes..

logcat的告诉我:

Logcat tells me:

01-16 13:42:15.863: DEBUG/dalvikvm(23161): GC_BEFORE_OOM freed 10K, 9% free 59019K/64400K, paused 29ms, total 30ms
01-16 13:42:15.863: ERROR/dalvikvm-heap(23161): Out of memory on a 8018704-byte allocation.
01-16 13:42:15.863: ERROR/AndroidRuntime(23161): FATAL EXCEPTION: main
        java.lang.OutOfMemoryError
        at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
        at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:502)
        at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:355)
        at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:785)
        at android.content.res.Resources.loadDrawable(Resources.java:1965)
        at android.content.res.Resources.getDrawable(Resources.java:660)
        at android.widget.ImageView.resolveUri(ImageView.java:616)
        at android.widget.ImageView.setImageResource(ImageView.java:349)
        at <MyApp>.MyActivity$6.run(MyActivity.java:143)
        at android.os.Handler.handleCallback(Handler.java:725)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5039)
        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:793)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
        at dalvik.system.NativeStart.main(Native Method)

有人可以给我一些提示如何处理崩溃? 也许它是因为我activitys被置为状态暂停,而不是正确地关闭它们?

Someone can give my some tips how to handle the crashes? Maybe its because my activitys are set to state "paused" instead of closing them correctly?

推荐答案

为速战速决,您可以添加

for quick fix you can add

安卓largeHeap =真正的在清单中的应用程序标签

android:largeHeap="true" in your manifest application tag

请看这里:

我面对的OOM问题我的表在Kindle和角落

I face OOM problem for my table in kindle and nook

Heap size (large)
android:largeHeap="true"

有规范提及在这里使用更大的堆链接:

编辑:使用位图缓存技术链接这里