gpt4 book ai didi

android - 位图选项 inSampleSize = 1 时应用程序崩溃

转载 作者:太空狗 更新时间:2023-10-29 14:20:27 27 4
gpt4 key购买 nike

我正在手机(尺寸 720x1280)上测试该应用。当我使用 2 的样本大小时,该应用程序运行良好。当我尝试使用 1 的样本大小时,该应用程序在我绘制图像的行中崩溃(下面提到的代码)。请指出我的代码需要更正的地方。

            canvas.drawBitmap(backgoundImage, 0, 0  , null);  

代码

    public  Bitmap getAssetImage(Context context, String filename) throws IOException {
AssetManager assets = getApplicationContext().getResources().getAssets();
InputStream buffer = null;
try {
buffer = new BufferedInputStream((assets.open("drawable/" + filename + ".png")));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

BitmapFactory.Options options = new BitmapFactory.Options();
options.inPurgeable = true;

if (tabletSize) {
Log.i("DragDrop", "am tablet");
} else {
Log.i("DragDrop", "am phone");
options.inSampleSize = 1;
}


Bitmap temp = BitmapFactory.decodeStream(buffer, null, options);
Bitmap finalImage = Bitmap.createScaledBitmap(temp, (int) dWidth, (int) dHeight, true);
temp.recycle();
temp=null;
return finalImage;

}

日志猫

07-07 12:28:14.150: E/AndroidRuntime(7256): FATAL EXCEPTION: Thread-748
07-07 12:28:14.150: E/AndroidRuntime(7256): java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@416ec9f0
07-07 12:28:14.150: E/AndroidRuntime(7256): at android.graphics.Canvas.throwIfRecycled(Canvas.java:1026)
07-07 12:28:14.150: E/AndroidRuntime(7256): at android.graphics.Canvas.drawBitmap(Canvas.java:1065)
07-07 12:28:14.150: E/AndroidRuntime(7256): at com.example.funandlearn.DragDrop$MyBringBackSurface.run(DragDrop.java:640)
07-07 12:28:14.150: E/AndroidRuntime(7256): at java.lang.Thread.run(Thread.java:856)

供您引用,第 640 行有代码

            canvas.drawBitmap(backgoundImage, 0, 0  , null);  

最佳答案

你有这个

    Bitmap temp = BitmapFactory.decodeStream(buffer, null, options);
Bitmap finalImage = Bitmap.createScaledBitmap(temp, (int) dWidth, (int) dHeight, true);
temp.recycle();
temp=null;

你的 logcat 说

    Canvas: trying to use a recycled bitmap

您应该在不使用时回收位图。

引用文档

在 Android 2.3.3(API 级别 10)及更低版本上,建议使用 recycle()。

只有当您确定该位图不再被使用时,您才应该使用recycle()。如果您调用 recycle() 并稍后尝试绘制位图,您将收到错误消息:“ Canvas :尝试使用回收的位图”。

在 Android 3.0 及更高版本上管理内存

Android 3.0(API 级别 11)引入了 BitmapFactory.Options.inBitmap 字段。 如果设置了此选项,采用 Options 对象的解码方法将在加载内容时尝试重用现有位图。无需使用 recycle

也检查这个

http://developer.android.com/training/displaying-bitmaps/manage-memory.html

关于android - 位图选项 inSampleSize = 1 时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17510020/

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