错误如下:
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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。