gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:51:52 45 4
gpt4 key购买 nike

我的应用的主要目的是按照图像所示的方式显示图像

enter image description here

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

}

详细代码可见here

在下一行抛出异常

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

最佳答案

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

Bitmap bitmap = Bitmap.createScaledBitmap(oldBitmap, newWidth, newHeight, true); 
oldBitmap.recycle();

正确的是:

Bitmap bitmap = Bitmap.createScaledBitmap(oldBitmap, newWidth, newHeight, true); 
if (oldBitmap!= bitmap){
oldBitmap.recycle();
}

因为文档说:

If the specified width and height are the same as the current width and height of the source btimap, the source bitmap is returned and now new bitmap is created.

关于Android Bitmap.createScaledBitmap 在 Jelly Bean 4.1 上抛出 java.lang.OutOfMemoryError moSTLy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15552076/

45 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com