Canvas: trying to use a recycled bitmap [email protected]

近期在做和图片相关显示的出现了一个问题,整理一下思路。分享出来给大家參考一下:

Exception Type:java.lang.RuntimeException
java.lang.RuntimeException: Canvas: trying to use a recycled bitmap [email protected] 	at android.graphics.Canvas.throwIfCannotDraw(Canvas.java:1282)
	at android.view.GLES20Canvas.drawBitmap(GLES20Canvas.java:599)
	at android.graphics.drawable.BitmapDrawable.draw(BitmapDrawable.java:538)
	at android.widget.ImageView.onDraw(ImageView.java:1176)
	at android.view.View.draw(View.java:15285)
	at android.view.View.updateDisplayListIfDirty(View.java:14208)
	at android.view.View.getDisplayList(View.java:14230)
	at android.view.View.draw(View.java:15013)
	at android.view.ViewGroup.drawChild(ViewGroup.java:3409)
	at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3202)
	at android.view.View.draw(View.java:15288)
	at android.widget.FrameLayout.draw(FrameLayout.java:598)
	at android.view.View.updateDisplayListIfDirty(View.java:14208)
	at android.view.View.getDisplayList(View.java:14230)
	at android.view.View.draw(View.java:15013)
	at android.view.ViewGroup.drawChild(ViewGroup.java:3409)
	at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3202)
	at android.view.View.updateDisplayListIfDirty(View.java:14203)
	at android.view.View.getDisplayList(View.java:14230)
	at android.view.View.draw(View.java:15013)
	at android.view.ViewGroup.drawChild(ViewGroup.java:3409)
	at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3202)
	at android.view.View.updateDisplayListIfDirty(View.java:14203)
	at android.view.View.getDisplayList(View.java:14230)
	at android.view.View.draw(View.java:15013)
	at android.view.ViewGroup.drawChild(ViewGroup.java:3409)
	at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3202)
	at android.view.View.updateDisplayListIfDirty(View.java:14203)
	at android.view.View.getDisplayList(View.java:14230)
	at android.view.View.draw(View.java:15013)
	at android.view.ViewGroup.drawChild(ViewGroup.java:3409)
	at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3202)
	at android.view.View.updateDisplayListIfDirty(View.java:14203)
	at android.view.View.getDisplayList(View.java:14230)
	at android.view.View.draw(View.java:15013)
	at android.view.ViewGroup.drawChild(ViewGroup.java:3409)
	at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3202)
	at android.view.View.updateDisplayListIfDirty(View.java:14203)
	at android.view.View.getDisplayList(View.java:14230)
	at android.view.View.draw(View.java:15013)
	at android.view.ViewGroup.drawChild(ViewGroup.java:3409)
	at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3202)
	at android.view.View.draw(View.java:15288)
	at android.widget.FrameLayout.draw(FrameLayout.java:598)
	at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2799)
	at android.view.View.updateDisplayListIfDirty(View.java:14208)
	at android.view.View.getDisplayList(View.java:14230)
	at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:273)
	at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:279)
	at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:318)
	at android.view.ViewRootImpl.draw(ViewRootImpl.java:2552)
	at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2368)
	at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1998)
	at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1077)
	at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5966)
	at android.view.Choreographer$CallbackRecord.run(Choreographer.java:786)
	at android.view.Choreographer.doCallbacks(Choreographer.java:599)
	at android.view.Choreographer.doFrame(Choreographer.java:569)
	at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:772)
	at android.os.Handler.handleCallback(Handler.java:743)
	at android.os.Handler.dispatchMessage(Handler.java:95)
	at android.os.Looper.loop(Looper.java:135)
	at android.app.ActivityThread.main(ActivityThread.java:5299)
	at java.lang.reflect.Method.invoke(Native Method)
	at java.lang.reflect.Method.invoke(Method.java:372)
	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)

基本的跟踪方向

at android.view.GLES20Canvas.drawBitmap(GLES20Canvas.java:599)
	at android.graphics.drawable.BitmapDrawable.draw(BitmapDrawable.java:538)
	at android.widget.ImageView.onDraw(ImageView.java:1176)

在这ImageView和BitmapDrawable、GLES20Canvas三个类报出异常信息。在找到ImageView代码中,发现了OnDraw的方法的mDrawable.draw(canvas);这一句出现了错

有接着在BitmapDrawable的OnDraw方法发现了错误行canvas.drawBitmap(bitmap, null, mDstRect, paint);:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" >

又相应在找到GLES20Canvas.java:599的问题:

立即找到了问题所在:

 protected static void throwIfCannotDraw(Bitmap bitmap) {
        if (bitmap.isRecycled()) {
            throw new RuntimeException("Canvas: trying to use a recycled bitmap " + bitmap);
        }
        if (!bitmap.isPremultiplied() && bitmap.getConfig() == Bitmap.Config.ARGB_8888 &&
                bitmap.hasAlpha()) {
            throw new RuntimeException("Canvas: trying to use a non-premultiplied bitmap "
                    + bitmap);
        }
    }

因此:能够说明:假设bitmap对象已经被回收了,可是BitmapDrawable的对象还在Canvas上绘制导致了跑出了该异常情况。

解决方式:

1、能够写一个集成ImageView的子类,单后复写onDraw()方法捕获此异常:

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		try{
			super.onDraw(canvas);
		}catch(Exception e){
		}
	}

2、在使用BitmapDrawable对象的时候能够,进行先将传递的bitmap对象推断。并在显示图片之前不要进行回收,直到使用完在回收。这样的不建议,由于图片多会出现OOM

时间: 07-07

Canvas: trying to use a recycled bitmap [email protected]的相关文章

关于Canvas: trying to use a recycled bitmap android.graphics的疑惑

============问题描述============ 因为viewpager图片内存溢出的问题,不得不考虑手动释放内存,不过出的问题我不理解. 我的想法是创建一个Map,然后用instantiateItem中的参数arg0当作键,bitmap当作值,当destroyItem中去掉VIew的时候我捎带着把不再用到的bitmap也回收掉 //这个是存bitmap的map public HashMap<Integer, SoftReference<Bitmap>> cacheBit;

Android手动回收bitmapisRecycled(),引发Canvas: trying to use a recycled bitmap处理

通过Imageview显示网络传回来的byte数据,发现内存会不断增大,最终导致内存溢出.于是手动去回收Bitmap的数据,发现会main ex (Canvas: trying to use a recycled bitmap) 网上查询下,发现重新Imageview的onDraw时,在super执行try catch,发现解决了问题,这样不必手动bitmap.recycled(),不然视频会闪动. 查询资料: http://blog.csdn.net/primer_programer/arti

【[email&#160;protected]基础篇 ~】# 磁盘与文件系统

之前三篇文章我们简单介绍了Linux系统的用户管理,文件操作等,都是比较浅显的基本操作.这节我们要深入一下了,从文件系统我们要看到磁盘系统.从磁盘系统我们要看到操作系统的整体架构.废话不多少让我们开始学习吧! 磁盘与文件系统 1.磁盘系统 1.1 磁盘结构 如图所示,磁盘由扇区和柱面组成,分区的最小单位是柱面(柱是有厚度的,本图是截面图),磁盘读取的最小单位是扇区.第一扇区的MBR(446bytes)分区表可以最大包含四个分区(64bytes)的信息,即从开始柱面到结束柱面4组数据,每组16个字

$*和[email&#160;protected]之间区别代码分析

#!/bin/bash set 'apple pie' pears peaches for i in $*           /*单引号被去掉,循环单个字符输出*/ do echo $i done [[email protected] Ex_14.02-14.31]# sh 14-14-1 apple pie pears peaches -------------------------------------------------------------- #!/bin/bash set

[email&#160;protected]一个高效的配置管理工具--Ansible configure management--翻译(六)

无书面许可请勿转载 高级playbook Finding files with variables All modules can take variables as part of their arguments by dereferencing them with {{ and }} . You can use this to load a particular file based on a variable. For example, you might want to select a

【转载】 ERROR 1045 (28000): Access denied for user [email&#160;protected] (using password: NO)

来自:http://www.jb51.net/LINUXjishu/10981.html 错误描述: Mysql中添加用户之后可能出现登录时提示ERROR 1045 (28000): Access denied for user的错误.删除user.user中值为NULL的,或更新NULL为test 1)delete from user where user is NULL 2)update user set user='test' where user is NULL.意外的情况: 如果上述方

[[email&#160;protected]] Omit catch error block if not needed

From [email protected], you can omit catch error block. Before: try { throw new Error('whatever'); } catch(err) { console.log(err) } Now: try { throw new Error('whatever'); } catch { console.log("error happened") } It is just a syntax sugar, if

[email&#160;protected]动态代理-类加载器

一.测试单元     概述:用于测试JAVA代码的工具类,已内置在Eclipse中;     格式:         1.在方法的上面添加@Test;         2.对被测试的方法的要求:权限-public;返回值-void;参数-空参         [email protected]:在@Test标注的方法前执行,可以用于初始化;           @After:在@Test标注的方法后执行,可以用于释放资源; 二.注解     概述:java的一种数据类型,和类/接口在同一级别  

git push报错error: failed to push some refs to &#39;[email&#160;protected]:

$ git push -u origin master To [email protected]:xxx/xxx.git ! [rejected] master -> master (fetch first) error: failed to push some refs to '[email protected]:xxx/xxx.git' hint: Updates were rejected because the remote contains work that you do hint: