溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Bug 3 :圖片縮略圖的問(wèn)題

發(fā)布時(shí)間:2020-08-08 01:41:05 來(lái)源:網(wǎng)絡(luò) 閱讀:416 作者:mama100Tech 欄目:移動(dòng)開(kāi)發(fā)

錯(cuò)誤如下:

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)

   

然后定位到這個(gè)代碼段:

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這個(gè)方法出了問(wèn)題

原文是這么說(shuō)的

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

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);

        }

    }

因此,需要在以前寫(xiě)的程序中,加入異常捕獲,程序才運(yùn)行正常。

或者加入判斷 :

修改前:

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;

                            }

        }


向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI