Android Bitmap.createScaledBitmap主要在Jelly Bean 4.1上抛出java.lang.OutOfMemoryError

RDX*_*RDX 11 graphics android exception bitmap out-of-memory

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

在此输入图像描述

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

}
Run Code Online (Sandbox Code Playgroud)

详细代码可以在这里找到

在以下行引发异常

Bitmap b = Bitmap.createScaledBitmap(bd.getBitmap(), (int) (bd.getIntrinsicHeight() * 0.9), (int) (bd.getIntrinsicWidth() * 0.7), false);
Run Code Online (Sandbox Code Playgroud)

以上函数调用自onItemSelected.**该应用程序仍然在2.2和2.3上运行良好,但在4.1上立即抛出异常上面的代码工作正常,但抛出异常.我没有看到2.2和2.3中的任何崩溃,但它在4.1中立即崩溃了果冻豆中的内存管理是否有任何重大差异?**:

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)
Run Code Online (Sandbox Code Playgroud)

Mal*_*asz 26

重要的是要注意以下代码可能导致异常:

Bitmap bitmap = Bitmap.createScaledBitmap(oldBitmap, newWidth, newHeight, true); 
oldBitmap.recycle();
Run Code Online (Sandbox Code Playgroud)

适当的是:

Bitmap bitmap = Bitmap.createScaledBitmap(oldBitmap, newWidth, newHeight, true); 
if (oldBitmap!= bitmap){
     oldBitmap.recycle();
}
Run Code Online (Sandbox Code Playgroud)

因为文件说:

如果指定的宽度和高度与源btimap的当前宽度和高度相同,则返回源位图,现在创建新的位图.


Rag*_*dan 13

http://www.youtube.com/watch?v=_CruQY55HOk.在andorid 3.0位图之后,像素数据存储在堆上.看来你超出了堆内存大小.仅仅因为你的应用需要大堆不使用大堆.更多堆的大小,更常规的垃圾收集.该视频对该主题有很好的解释.

还可以在不使用时回收位图.堆上的垃圾收集完成了我的标记和扫描,因此当您回收位图时它会释放内存.因此,您的堆大小不会增长并且内存不足.

 bitmap.recycle();
Run Code Online (Sandbox Code Playgroud)

http://developer.android.com/training/displaying-bitmaps/load-bitmap.html.有效加载位图的文档.看看在内存中加载缩小版本.

除此之外,您还可以使用Universal Image Loader.https://github.com/nostra13/Android-Universal-Image-Loader.

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

两者都使用缓存.


Tal*_*lha 5

您正在尝试访问更多的内存。尝试使用

 BitmapFactory.Options opts=new BitmapFactory.Options();
        opts.inDither=false;                    
        opts.inSampleSize = 8;                   
        opts.inPurgeable=true;                 
        opts.inInputShareable=true;             
        opts.inTempStorage=new byte[16 * 1024]; 

Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.h1)
        , 65,65, true),
Run Code Online (Sandbox Code Playgroud)

另外请看下面的链接以增加内存

http://developer.android.com/reference/android/R.styleable.html#AndroidManifestApplication_largeHeap

在Android中检测应用程序堆大小

编辑1

尝试使用nostras图像下载器,您可以使用它在本地存储中显示图像。而且它可以很好地管理内存...

https://github.com/nostra13/Android-Universal-Image-Loader