Android IllegalArgumentException: Cannot draw recycled bitmaps解决方法

时间:2023-03-08 20:45:14

在编码图集过程中,出现了Android IllegalArgumentException: Cannot draw recycled bitmaps错误。

大致意思是:不能使用已经被回收的bitmap。

bitmap回收部分代码如下:

 Bitmap removeBitmap = softReference.get();
if(removeBitmap != null && !removeBitmap.isRecycled()){
removeBitmap.recycle();
removeBitmap = null;
}

解决方法:

重写ImageView中的OnDraw方法,捕获此异常。

 public class MyImageView extends ImageView {  

     public MyImageView (Context context, AttributeSet attrs) {
super(context, attrs);
} @Override
protected void onDraw(Canvas canvas) {
try {
super.onDraw(canvas);
} catch (Exception e) {
System.out.println("trying to use a recycled bitmap");
}
}