分类: Android平台
2013-09-17 11:42:00
转http://blog.csdn.net/lquanshui/article/details/8235824
java.lang.IllegalArgumentException: Cannot draw recycled bitmapsat android.view.GLES20Canvas.drawBitmap(GLES20Canvas.java:778)at android.view.GLES20RecordingCanvas.drawBitmap(GLES20RecordingCanvas.java:117)at android.graphics.drawable.BitmapDrawable.draw(BitmapDrawable.java:393)at android.widget.ImageView.onDraw(ImageView.java:979)at android.view.View.draw(View.java:13458)at android.view.View.getDisplayList(View.java:12409)at android.view.View.getDisplayList(View.java:12453)at android.view.View.draw(View.java:13182)at android.view.ViewGroup.drawChild(ViewGroup.java:2929)at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2799)at android.view.View.draw(View.java:13461)at android.view.View.getDisplayList(View.java:12409)at android.view.View.getDisplayList(View.java:12453)at android.view.View.draw(View.java:13182)at android.view.ViewGroup.drawChild(ViewGroup.java:2929)at android.widget.ListView.drawChild(ListView.java:3226)at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2799)at android.widget.AbsListView.dispatchDraw(AbsListView.java:2433)at android.widget.ListView.dispatchDraw(ListView.java:3221)at android.view.View.draw(View.java:13461)at android.widget.AbsListView.draw(AbsListView.java:3759)
经分析,引起原因:
android4.0和android4.1的api差异,Bitmap在创建缩略图时,4.1.1的时候,若缩略图和原图大小一样,创建的缩略图会返回原图,若原图的bitmap人为的回收或者系统回收,就会引起此异常。
GLES20Canvas相关源码如下:
android 4.0
public void drawBitmap(Bitmap bitmap, float left, float top, Paint paint) {
// Shaders are ignored when drawing bitmaps
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 new IllegalArgumentException("Cannot draw recycled bitmaps");
// Shaders are ignored when drawing bitmaps
int modifiers = paint != null ? setupModifiers(bitmap, paint) : MODIFIER_NONE;
try {
final int nativePaint = paint == null ? 0 : paint.mNativePaint;
nDrawBitmap(mRenderer, bitmap.mNativeBitmap, bitmap.mBuffer, left, top, nativePaint);
} finally {
if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers);
}
}
因此,需要在以前写的程序中,加入异常捕获,程序才运行正常。