码迷,mamicode.com
首页 > 其他好文 > 详细

Bug 3 :图片缩略图的问题

时间:2015-06-09 14:12:09      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:android 图片 缩略图

错误如下:

Caused by: java.lang.IllegalArgumentException: Cannot draw recycled bitmaps

2    at android.view.GLES20Canvas.drawBitmap(GLES20Canvas.java:794)

3    at android.view.GLES20RecordingCanvas.drawBitmap(GLES20RecordingCanvas.java:117)

   

然后定位到这个代码段:

Bitmap thumbBmp = null;

      if (tmpBitmap != null && !tmpBitmap.isRecycled()) {

         thumbBmp= Bitmap.createScaledBitmap(tmpBitmap,

                tmpBitmap.getWidth()/ 2, tmpBitmap.getHeight() / 2, true);

         if (tmpBitmap != null &&!tmpBitmap.isRecycled()) {

            tmpBitmap.recycle();

            tmpBitmap= null;

         }

      }

 

其中是createScaledBitmap这个方法出了问题

原文是这么说的

"Creates a new bitmap, scaled froman existing bitmap, when possible. If the specified width andheight are the same as the current width and height of the source bitmap, thesource bitmap is returned and no new bitmap is created."


源码:

public static Bitmap createScaledBitmap(Bitmapsrc, intdstWidth, intdstHeight,

            boolean filter) {

        Matrix m;

        synchronized (Bitmap.class) {

            // small pool of just 1 matrix

            m = sScaleMatrix;

            sScaleMatrix = null;

        }

 

        if (m == null) {

            m = new Matrix();

        }

 

        finalint width = src.getWidth();

        finalint height = src.getHeight();

        finalfloat sx = dstWidth  / (float)width;

        finalfloat sy = dstHeight / (float)height;

        m.setScale(sx, sy);

        Bitmap b = Bitmap.createBitmap(src,0, 0, width, height, m, filter);

 

        synchronized (Bitmap.class) {

            // do we need to check for null? why not just assign everytime?

            if (sScaleMatrix == null) {

                sScaleMatrix = m;

            }

        }

 

        return b;

   }


其中,android4.0和android4.1api还有差异,Bitmap在创建缩略图时,4.1.1的时候,若缩略图和原图大小一样,创建的缩略图会返回原图,若原图的bitmap人为的回收或者系统回收,就会引起此异常。

GLES20Canvas相关源码如下:

android 4.0

   public void drawBitmap(Bitmap bitmap, float left, float top, Paint paint) {

        // Shaders are ignored when drawingbitmaps

        int modifiers = paint != null ?setupModifiers(bitmap, paint) : MODIFIER_NONE;

        final int nativePaint = paint ==null ? 0 : paint.mNativePaint;

        nDrawBitmap(mRenderer,bitmap.mNativeBitmap, bitmap.mBuffer, left, top, nativePaint);

        if (modifiers != MODIFIER_NONE)nResetModifiers(mRenderer, modifiers);

    }

android4.1

   public void drawBitmap(Bitmap bitmap, float left, float top, Paint paint) {

        if (bitmap.isRecycled()) throw newIllegalArgumentException("Cannot draw recycled bitmaps");

       // Shaders are ignored when drawing bitmaps

        int modifiers = paint != null ?setupModifiers(bitmap, paint) : MODIFIER_NONE;

        try {

            final intnativePaint = paint == null ? 0 : paint.mNativePaint;

            nDrawBitmap(mRenderer,bitmap.mNativeBitmap, bitmap.mBuffer, left, top, nativePaint);

        } finally {

            if(modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers);

        }

    }

因此,需要在以前写的程序中,加入异常捕获,程序才运行正常。

或者加入判断 :

修改前:

Bitmapthumbnail = Bitmap.createScaledBitmap(bmp,w, h, true);

if (!thumbnail.equals(bmp)){

    if (!bmp.isRecycled()) {

        bmp.recycle();

    }

    bmp = null;

}

 

修改后:

    if (tmpBitmap != null &&!tmpBitmap.isRecycled()) {

            thumbBmp = Bitmap.createScaledBitmap(tmpBitmap,

                    tmpBitmap.getWidth() / 2,tmpBitmap.getHeight() / 2, true);

            if (!thumbBmp.equals(tmpBitmap) &&!tmpBitmap.isRecycled()) {

                                        tmpBitmap.recycle();

                tmpBitmap = null;

                            }

        }


Bug 3 :图片缩略图的问题

标签:android 图片 缩略图

原文地址:http://mama100tech.blog.51cto.com/8943983/1659684

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!