您好,登錄后才能下訂單哦!
錯誤如下:
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;
}
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。