Bug3:图片缩略图的问题-创新互联
错误如下:
创新互联是一家专业提供磁县企业网站建设,专注与成都网站建设、成都做网站、H5网站设计、小程序制作等业务。10年已为磁县众多企业、政府机构等服务。创新互联专业网站建设公司优惠进行中。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;
}
}
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
名称栏目:Bug3:图片缩略图的问题-创新互联
路径分享:http://scjbc.cn/article/dogpis.html