【发布时间】:2013-07-07 07:09:30
【问题描述】:
我正在手机上测试应用程序(尺寸 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;
}
LogCat
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);
【问题讨论】:
-
它说你已经回收了位图并且你正在使用它。
-
你的安卓版本是多少?
-
youtube.com/watch?v=_CruQY55HOk。查看此链接了解更多详情
标签: android android-canvas android-image