zoukankan      html  css  js  c++  java
  • Android java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@412d7230

    近期遇到了如标题这种错误,再次记录解决方法。本文參考帖子:

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

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

    top=(ImageView)view.findViewById(R.id.top);
            bitmap=ImgBitmap.comeFromRes(getResources(), R.drawable. top);
            top.setImageBitmap(bitmap);
            bottom=(ImageView)view.findViewById(R.id.bottom);
            bitmap1=ImgBitmap.comeFromRes(getResources(), R.drawable. bottom);
            bottom.setImageBitmap(bitmap1);

    在回收时,应该这样写:

        @Override
        public void onDestroy() {
            // TODO Auto-generated method stub
            top.setImageBitmap(null);
            bottom.setImageBitmap(null);
            ImgBitmap.MyRecycle(bitmap);
            ImgBitmap.MyRecycle(bitmap1);
            super.onDestroy();
        }

    或者

        @Override
        public void onDestroy() {
            // TODO Auto-generated method stub
            ImgBitmap.MyRecycle(mBitmap);
            ImgBitmap.MyRecycle(mBitmap1);
            top=null;
            bottom=null;
            super.onDestroy();
         }

    另外MyRecycle方法的代码例如以下:

    public static void MyRecycle(Bitmap bmp){
        if(!bmp.isRecycled() && null!=bmp){
            bmp=null;
        }
    }

    总之是必需要解除与bitmap有关的全部绑定。

  • 相关阅读:
    257. Binary Tree Paths
    324. Wiggle Sort II
    315. Count of Smaller Numbers After Self
    350. Intersection of Two Arrays II
    295. Find Median from Data Stream
    289. Game of Life
    287. Find the Duplicate Number
    279. Perfect Squares
    384. Shuffle an Array
    E
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/7874865.html
Copyright © 2011-2022 走看看