Android的Bitmap.createScaledBitmap抛出java.lang.OutOfMemoryError主要是在果冻豆4.1 [英] Android Bitmap.createScaledBitmap throws java.lang.OutOfMemoryError mostly on Jelly Bean 4.1

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

问题描述

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

 私人无效setSelectedImage(INT selectedImagePosition)
{

    BitmapDrawable BD =(BitmapDrawable)drawables.get(selectedImagePosition);
    位图B = Bitmap.createScaledBitmap(bd.getBitmap(),(INT)(bd.getIntrinsicHeight()* 0.9),(INT)(bd.getIntrinsicWidth()* 0.7),FALSE);
    selectedImageView.setImageBitmap(B);
    selectedImageView.setScaleType(ScaleType.FIT_XY);

}
 

详细code可以找到这里

抛出异常,在下面的行

 位图B = Bitmap.createScaledBitmap(bd.getBitmap(),(INT)(bd.getIntrinsicHeight()* 0.9),(INT)(bd.getIntrinsicWidth()* 0.7) , 假);
 

以上函数是从 onItemSelected 调用。 *的的应用程序仍然运作良好的2.2和2.3,但4.1立即引发异常以上code正常工作,但会引发以下异常。我因此未看到任何崩溃的2.2和2.3,但它immedidately崩溃在4.1是否存在内存管理的糖豆任何重大的区别? * 的:

  java.lang.OutOfMemoryError
AndroidRuntime(2616):在android.graphics.Bitmap.nativeCreate(本机方法)
AndroidRuntime(2616):在android.graphics.Bitmap.createBitmap(Bitmap.java:640)
AndroidRuntime(2616):在android.graphics.Bitmap.createBitmap(Bitmap.java:586)
AndroidRuntime(2616):在android.graphics.Bitmap.createScaledBitmap(Bitmap.java:466)
AndroidRuntime(2616):在com.rdx.gallery.GalleryDemoActivity.setSelectedImage(GalleryDemoActivity.java:183)
 

解决方案

http://www.youtube.com/watch ?V = _CruQY55HOk 。后的And​​orid 3.0的位图像素数据被存储在堆上。看来你是超出堆内存的大小。仅仅因为你的应用程序需要大量的堆不使用大堆。堆更多的大小,更经常的垃圾收集。视频对主题的一个很好的解释。

另外回收位图时不使用。堆的垃圾收集完成我的标记和清除,所以当你回收位图,它自由的内存。所以,你的堆大小不会增长和耗尽内存。

  bitmap.recycle();
 

<一个href="http://developer.android.com/training/displaying-bitmaps/load-bitmap.html">http://developer.android.com/training/displaying-bitmaps/load-bitmap.html.在装载位图文件有效。看一看加载缩小版在内存中。

除了形成这个,你可以使用通用图像装载机。 <一href="https://github.com/nostra13/Android-Universal-Image-Loader">https://github.com/nostra13/Android-Universal-Image-Loader.

https://github.com/thest1/LazyList 。懒图像加载。

两者都使用缓存。

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

}

Detailed code can be find here

exception is thrown at following line

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

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)

解决方案

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.

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

https://github.com/thest1/LazyList. Lazy Loading of Images.

Both use caching.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆