OutOfMemoryError主要在Jelly

OutOfMemoryError主要在Jelly

本文介绍了Android Bitmap.createScaledBitmap抛出java.lang.OutOfMemoryError主要在Jelly Bean 4.1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序的主要目的是以如下所示的方式显示图像

My app main purpose is to display images in following fashion as shown in image

private void setSelectedImage(int selectedImagePosition)
{

    BitmapDrawable bd = (BitmapDrawable) drawables.get(selectedImagePosition);
    Bitmap b = Bitmap.createScaledBitmap(bd.getBitmap(), (int) (bd.getIntrinsicHeight() * 0.9), (int) (bd.getIntrinsicWidth() * 0.7), false);
    selectedImageView.setImageBitmap(b);
    selectedImageView.setScaleType(ScaleType.FIT_XY);

}

详细代码可以找到

异常是抛出下列行

Bitmap b = Bitmap.createScaledBitmap(bd.getBitmap(), (int) (bd.getIntrinsicHeight() * 0.9), (int) (bd.getIntrinsicWidth() * 0.7), false);

以上功能从 onItemSelected 调用。 **该应用程序在2.2和2.3版本仍然运行良好,但在4.1上立即抛出异常上面的代码工作正常,但会抛出以下异常。我没有看到2.2和2.3中的任何崩溃,但是它在4.1中崩溃在Jelly bean中内存管理有什么重大差异? **:

Above function is called from onItemSelected. **The app still works well on 2.2 and 2.3, but throws exception immediately on 4.1 Above code works fine, but throws following exception. I didnot see any crashes in 2.2 and 2.3, but it immedidately crashes in 4.1 Is there any major difference of memory management in Jelly beans? **:

java.lang.OutOfMemoryError
AndroidRuntime(2616):   at android.graphics.Bitmap.nativeCreate(Native Method)
AndroidRuntime(2616):   at android.graphics.Bitmap.createBitmap(Bitmap.java:640)
AndroidRuntime(2616):   at android.graphics.Bitmap.createBitmap(Bitmap.java:586)
AndroidRuntime(2616):   at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:466)
AndroidRuntime(2616):   at com.rdx.gallery.GalleryDemoActivity.setSelectedImage(GalleryDemoActivity.java:183)


推荐答案

。 afterorid 3.0位图像素数据存储在堆上。似乎你超过了堆内存大小。只是因为你的应用需要大堆不要使用大堆。更多的堆大小,更规则的垃圾收集。该视频对此主题有很好的解释。

http://www.youtube.com/watch?v=_CruQY55HOk. After andorid 3.0 bitmaps pixel data are stored on the heap. It seems you are exceeding heap memory size. Just because your app requires large heap do not use large heap. More the size of heap, more regular garbage collections. The video has a good explanation on the topic.

在不使用时还会回收位图。堆上的垃圾收集完成了我的标记和扫描,所以当你回收位图时,它的内存空闲。所以你的堆大小不会增长,并且内存不足。

Also recycle bitmaps when not in use. Garbage collections on heap is done my mark and sweep , so when you recycle bitmaps it free's memory. So your heap size will not grow and run out of memory.

 bitmap.recycle();

。有效加载位图的文档。看看在内存中加载缩小版本。

http://developer.android.com/training/displaying-bitmaps/load-bitmap.html. Documentation on loading bitmaps efficiently. Have a look at loading scaled down version in memory.

除此之外,您还可以使用Universal Image Loader。 。

Apart form this you can use Universal Image Loader. https://github.com/nostra13/Android-Universal-Image-Loader.

。懒惰加载图片。

都使用缓存。

这篇关于Android Bitmap.createScaledBitmap抛出java.lang.OutOfMemoryError主要在Jelly Bean 4.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 07:16