java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@412d723

来源:互联网 发布:广东裤哥战哭淘宝女郎 编辑:程序博客网 时间:2024/06/05 20:35
最近遇到了如标题这样的错误,再次记录解决方法。本文参考帖子:

http://bbs.csdn.net/topics/390196217

出现此bug的原因是在内存回收上,里面用Bitamp的代码为:

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. top=(ImageView)view.findViewById(R.id.top);  
  2.         bitmap=ImgBitmap.comeFromRes(getResources(), R.drawable. top);  
  3.         top.setImageBitmap(bitmap);  
  4.         bottom=(ImageView)view.findViewById(R.id.bottom);  
  5.         bitmap1=ImgBitmap.comeFromRes(getResources(), R.drawable. bottom);  
  6.         bottom.setImageBitmap(bitmap1);  

在回收时,应该这样写:




[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. @Override  
  2.     public void onDestroy() {  
  3.         // TODO Auto-generated method stub  
  4.         top.setImageBitmap(null);  
  5.         bottom.setImageBitmap(null);  
  6.         ImgBitmap.MyRecycle(bitmap);  
  7.         ImgBitmap.MyRecycle(bitmap1);  
  8.         super.onDestroy();  
  9.           
  10.               
  11.           
  12.           
  13.     }  

或者

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. @Override  
  2.     public void onDestroy() {  
  3.         // TODO Auto-generated method stub  
  4.         ImgBitmap.MyRecycle(mBitmap);  
  5.         ImgBitmap.MyRecycle(mBitmap1);  
  6.         top=null;  
  7.         bottom=null;  
  8.           
  9.         super.onDestroy();  
  10.           
  11.               
  12.           
  13.           
  14.     }  

另外MyRecycle方法的代码如下:
[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. public static void MyRecycle(Bitmap bmp){  
  2.         if(!bmp.isRecycled() && null!=bmp){  
  3.                 bmp=null;  
  4.         }  
  5.         }  
  6.     }  
  7.       
总之是必须要解除与bitmap有关的所有绑定。

转载请注明出处:http://blog.csdn.net/android_jiangjun/article/details/39698459


0 0
原创粉丝点击