我正在手机上测试应用程序(尺寸 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);