Android IllegalArgumentException: Cannot draw recycled bitmaps解决方法

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

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

bitmap回收部分代码如下:

1 Bitmap removeBitmap = softReference.get();  
2 if(removeBitmap != null && !removeBitmap.isRecycled()){  
3     removeBitmap.recycle();
4     removeBitmap = null;  
5 } 

解决方法:

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

 1 public class MyImageView extends ImageView {  
 2   
 3     public MyImageView (Context context, AttributeSet attrs) {  
 4         super(context, attrs);  
 5     }  
 6   
 7     @Override  
 8     protected void onDraw(Canvas canvas) {  
 9         try {  
10             super.onDraw(canvas);  
11         } catch (Exception e) {  
12             System.out.println("trying to use a recycled bitmap");  
13         }  
14     }  

 

原文地址:https://www.cnblogs.com/lwbqqyumidi/p/3407626.html