Bug3:圖片縮略圖的問題

錯誤如下:

創(chuàng)新互聯(lián)主要從事網(wǎng)站設(shè)計、成都網(wǎng)站設(shè)計、網(wǎng)頁設(shè)計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)南縣,10多年網(wǎng)站建設(shè)經(jīng)驗,價格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):13518219792

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在創(chuàng)建縮略圖時,4.1.1的時候,若縮略圖和原圖大小一樣,創(chuàng)建的縮略圖會返回原圖,若原圖的bitmap人為的回收或者系統(tǒng)回收,就會引起此異常。

GLES20Canvas相關(guān)源碼如下:

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;

                            }

        }

新聞名稱:Bug3:圖片縮略圖的問題
網(wǎng)站地址:http://muchs.cn/article30/jehjpo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作網(wǎng)頁設(shè)計公司、面包屑導(dǎo)航手機網(wǎng)站建設(shè)、App設(shè)計、網(wǎng)站設(shè)計公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

小程序開發(fā)