錯(cuò)誤如下:
創(chuàng)新互聯(lián)是一家專業(yè)提供磁縣企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站建設(shè)、成都做網(wǎng)站、H5網(wǎng)站設(shè)計(jì)、小程序制作等業(yè)務(wù)。10年已為磁縣眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站建設(shè)公司優(yōu)惠進(jìn)行中。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;
}
}
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。