大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
错误如下:
创新互联主要从事网站设计、成都网站设计、网页设计、企业做网站、公司建网站等业务。立足成都服务南县,10多年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务: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在创建缩略图时,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;
}
}