加载位图时出现内存不足错误

时间:2021-01-12 21:34:54

i have an android app with 3 acitivtys:

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

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

A1 --starts - > A2 --starts - > A3 - 完成他的过程后:开始 - > A1

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

(所以我不“完成();”一个应用程序。我用“startActivity(..);”用户交互后的整个时间开始下一个活动)

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:

所以这3个活动中有一个循环。在每个活动上,我显示3-9张图片,位于SD卡上,我使用以下功能加载:

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;
}

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

这一切都有效。但有时候(通过我的活动循环几次后),我的应用程序崩溃了..

Logcat tells me:

Logcat告诉我:

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)

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?

有人可以提供一些如何处理崩溃的提示吗?也许是因为我的活动设置为“暂停”而不是正确关闭它们?

4 个解决方案

#1


31  

for quick fix you can add

为了快速修复,您可以添加

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

你的清单应用程序标签中的android:largeHeap =“true”

link here:

链接到这里:

I face OOM problem for my table in kindle and nook

我的桌子在点燃和角落面临OOM问题

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

there specification is mentioned to use larger heap link here:

这里提到规范使用更大的堆链接:

edit: use Caching Bitmaps technique link here

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

#2


2  

I had the similar problem while displaying high resolution images. I have searched and applied all the android bitmap solutions given in http://developer.android.com/training/displaying-bitmaps/index.html and the following caches mekanism in the link. but none of them work. not any right solution anywhere. Then I figured out that the problem is : I couldnt use the drawable folder structure right. I was keeping high resolution images in mdpi and hdpi folders . this cause android scale up the images to the ultra sizes. 100 kb image was causing 20 mb increase in memory thanks to the android monitor / memory section. so I have added these ultra resolution images to xxhdpi folder , then It was FIXED suddenly. then my image slider work flawlessly

我在显示高分辨率图像时遇到了类似的问题。我已经搜索并应用了http://developer.android.com/training/displaying-bitmaps/index.html中提供的所有android位图解决方案,并在链接中缓存了mekanism。但它们都不起作用。没有任何正确的解决方案。然后我发现问题是:我无法使用drawable文件夹结构。我在mdpi和hdpi文件夹中保存了高分辨率图像。这导致android将图像缩小到超大尺寸。由于android监视器/内存部分,100 kb图像导致内存增加20 mb。所以我已将这些超分辨率图像添加到xxhdpi文件夹,然后它突然被修复了。然后我的图像滑块完美无瑕地工作

#3


0  

Yeah android doesnt immediately destroy activities when they are not visible to the user, but there are a number of lifecycle methods that are called depending on the current state. Check out http://developer.android.com/guide/components/activities.html#Lifecycle

是的,当android对用户不可见时,它不会立即销毁活动,但是根据当前状态调用了许多生命周期方法。查看http://developer.android.com/guide/components/activities.html#Lifecycle

You could try finishing the activities in onStop() or onDestroy() as these will be called when the activity is not visible and when the system runs low on memory respectively. :-)

您可以尝试完成onStop()或onDestroy()中的活动,因为当活动不可见时以及系统内存不足时,将调用这些活动。 :-)

#4


0  

This is due to the high resolution of image you have to scaling the bitmap use the following code

这是由于您必须使用以下代码缩放位图的高分辨率图像

 Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

    int h = 100; // height in pixels
    int w = 100; // width in pixels
Bitmap photoBitMap = Bitmap.createScaledBitmap(myBitmap,h, w, true);
ivTmp.setImageBitmap(photoBitMap);

#1


31  

for quick fix you can add

为了快速修复,您可以添加

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

你的清单应用程序标签中的android:largeHeap =“true”

link here:

链接到这里:

I face OOM problem for my table in kindle and nook

我的桌子在点燃和角落面临OOM问题

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

there specification is mentioned to use larger heap link here:

这里提到规范使用更大的堆链接:

edit: use Caching Bitmaps technique link here

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

#2


2  

I had the similar problem while displaying high resolution images. I have searched and applied all the android bitmap solutions given in http://developer.android.com/training/displaying-bitmaps/index.html and the following caches mekanism in the link. but none of them work. not any right solution anywhere. Then I figured out that the problem is : I couldnt use the drawable folder structure right. I was keeping high resolution images in mdpi and hdpi folders . this cause android scale up the images to the ultra sizes. 100 kb image was causing 20 mb increase in memory thanks to the android monitor / memory section. so I have added these ultra resolution images to xxhdpi folder , then It was FIXED suddenly. then my image slider work flawlessly

我在显示高分辨率图像时遇到了类似的问题。我已经搜索并应用了http://developer.android.com/training/displaying-bitmaps/index.html中提供的所有android位图解决方案,并在链接中缓存了mekanism。但它们都不起作用。没有任何正确的解决方案。然后我发现问题是:我无法使用drawable文件夹结构。我在mdpi和hdpi文件夹中保存了高分辨率图像。这导致android将图像缩小到超大尺寸。由于android监视器/内存部分,100 kb图像导致内存增加20 mb。所以我已将这些超分辨率图像添加到xxhdpi文件夹,然后它突然被修复了。然后我的图像滑块完美无瑕地工作

#3


0  

Yeah android doesnt immediately destroy activities when they are not visible to the user, but there are a number of lifecycle methods that are called depending on the current state. Check out http://developer.android.com/guide/components/activities.html#Lifecycle

是的,当android对用户不可见时,它不会立即销毁活动,但是根据当前状态调用了许多生命周期方法。查看http://developer.android.com/guide/components/activities.html#Lifecycle

You could try finishing the activities in onStop() or onDestroy() as these will be called when the activity is not visible and when the system runs low on memory respectively. :-)

您可以尝试完成onStop()或onDestroy()中的活动,因为当活动不可见时以及系统内存不足时,将调用这些活动。 :-)

#4


0  

This is due to the high resolution of image you have to scaling the bitmap use the following code

这是由于您必须使用以下代码缩放位图的高分辨率图像

 Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

    int h = 100; // height in pixels
    int w = 100; // width in pixels
Bitmap photoBitMap = Bitmap.createScaledBitmap(myBitmap,h, w, true);
ivTmp.setImageBitmap(photoBitMap);