关于bitmap recycle trying to use a recycled bitmap android.graphics.Bitmap

在开发中,一直使用4.0以上手机作为測试机所以一直没有出现这个问题,今天换了2.3版本号的手机。出现了这个错误:

trying to use a recycled bitmap android.graphics.Bitmap


后检查代码,我的图片回收代码是介个样子的:

public static void recycle(View view) {
if (null == view) {
return;
}


BitmapDrawable bd = (BitmapDrawable) view.getBackground();
if (null == bd) {
return;
}

Bitmap bm = bd.getBitmap();
if (null != bm && !bm.isRecycled()) {
bm.recycle();
}
}


即在我回收了图片后,这个View对象还在引用这个图片。导致出现这个问题,后改动为:

public static void recycle(View view) {
if (null == view) {
return;
}

BitmapDrawable bd = (BitmapDrawable) view.getBackground();
if (null == bd) {
return;
}


view.setBackgroundDrawable(null);
Bitmap bm = bd.getBitmap();
if (null != bm && !bm.isRecycled()) {
bm.recycle();
}
}


设置背景图片为空,取消对图片的引用再去回收。


【推广】 免费学中医,健康全家人
原文地址:https://www.cnblogs.com/yjbjingcha/p/8324881.html