关于BItmap.createScaledBitmap释放内存引发的bug

灰色流连 2014-12-04 11:57:27
加精
代码如下:
Bitmap bm = BitmapFactory.decodeResource(res, id);
Bitmap newBm = Bitmap.createScaledBitmap(bm , 100,100, true);
bm.recyle();
mImageView.setImageBitmap(newBm);

然后会报一个异常:
java.lang.IllegalArgumentException: Cannot draw recycled bitmaps


如果先mImageView.setImageBitmap(newBm); 再bm.recyle(); 还是一样报异常, 求解
...全文
5160 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
-上帝之手- 2015-10-25
  • 打赏
  • 举报
回复
三维游戏引擎开发-图形理论基础 --不调用opengl或者第三方api绘制三维场景 http://edu.csdn.net/course/detail/864 OpenGL ES2.0基础 http://edu.csdn.net/course/detail/958 OpenGL ES2.0中级篇(shader横行天下) http://edu.csdn.net/course/detail/1167 三维游戏引擎设计与实现-GUI设计与实现 http://edu.csdn.net/course/detail/1037 三维游戏引擎开发-渲染 http://edu.csdn.net/course/detail/606 太空大战游戏实战课程 http://edu.csdn.net/course/detail/763
timothylee2005 2015-08-12
  • 打赏
  • 举报
回复
引用 7 楼 Bokutake 的回复:
createScaledBitmap Creates a new bitmap, scaled from an existing bitmap, when possible. If the specified width and height are the same as the current width and height of the source btimap, the source bitmap is returned and now new bitmap is created.
7楼是正解,但是打错一个关键的单词 and now new bitmap is created ------应该是-----> and no new bitmap is created now------应该是-----> no 表示不会创建bitmap, 而不是现在创建bitmap
timothylee2005 2015-08-12
  • 打赏
  • 举报
回复
给bm.recyle()这句加个判断,应该就不会报错了. if(newBm !=bm ){ bm.recyle(); }
sk_lin 2015-07-29
  • 打赏
  • 举报
回复
引用 2 楼 u012156163 的回复:
我回收的是第一个bitmap imageview里面用的是第二个bitmap 不是同一个呀
你回收的其实是同一个对象! 你第二个Bitmap并不是new出来的,这里只是第一个Bitmap的引用。 所以第一个Bitmap回收后,第二个Bitmap指向的内存也被回收了。 至于你先setImageBitmap()再recycle(),则是因为setImageBitmap()调用时,并非立即重绘。而是先保存bitmap的引用,然后通知主线程去重绘,主线程收到消息后,再调用Bitmap引用重绘UI。 这时就会出现这个错误。
_xianfeng99 2014-12-18
  • 打赏
  • 举报
回复
应该是7楼说的 The new scaled bitmap or the source bitmap if no scaling is required. 楼主检查一下创建的和原图片的宽高是否一样,如果一样返回的就是原本的bitmap,不创建新的,bm和newBm引用的是一个bitmap.
灰色流连 2014-12-18
  • 打赏
  • 举报
回复
引用 8 楼 u012137924 的回复:
你可以New一个Canvas然后把Bm画上去再保存下,然后再回收Bm。 你上面的做法是把Bm当做参数传进去,然后,你又回收了这个Bm参数,所以系统给你报了非法参数的异常。
难道Bitmap.createScaledBitmap(bm , 100,100, true)这个方法是异步的吗,不然已经执行完了我回收应该没问题呀
灰色流连 2014-12-18
  • 打赏
  • 举报
回复
引用 14 楼 BuleRiver 的回复:
如果尺寸相同,会直接返回原来的图片。 你需要检查newBmp和bm是不是同一个图片。
这个不是, 我打印了两个地址 不一样
7号南孚电池 2014-12-15
  • 打赏
  • 举报
回复
引用 7 楼 Bokutake 的回复:
createScaledBitmap Creates a new bitmap, scaled from an existing bitmap, when possible. If the specified width and height are the same as the current width and height of the source btimap, the source bitmap is returned and now new bitmap is created.
+1
kisskk2010 2014-12-14
  • 打赏
  • 举报
回复
不错,资源可用。
kisskk2010 2014-12-14
  • 打赏
  • 举报
回复
学习中!!!!!!!!
BuleRiver 2014-12-14
  • 打赏
  • 举报
回复
如果尺寸相同,会直接返回原来的图片。 你需要检查newBmp和bm是不是同一个图片。
Trent1985 2014-12-08
  • 打赏
  • 举报
回复
代码没错,不过少了一句: Bitmap bm = BitmapFactory.decodeResource(res, id); Bitmap newBm = Bitmap.createScaledBitmap(bm , 100,100, true); bm.recyle(); mImageView.setImageBitmap(null);//使用之前一定要先把ImageView清空,要不然会出现你所述的异常 mImageView.setImageBitmap(newBm);
辰岡墨竹 2014-12-06
  • 打赏
  • 举报
回复
createScaledBitmap Creates a new bitmap, scaled from an existing bitmap, when possible. If the specified width and height are the same as the current width and height of the source btimap, the source bitmap is returned and now new bitmap is created.
山鹰1985 2014-12-06
  • 打赏
  • 举报
回复
你可以New一个Canvas然后把Bm画上去再保存下,然后再回收Bm。 你上面的做法是把Bm当做参数传进去,然后,你又回收了这个Bm参数,所以系统给你报了非法参数的异常。
Birds2018 2014-12-04
  • 打赏
  • 举报
回复
他需要bitmap对象绘制到UI上,这个对象你回收了 就无法绘制了,其实是一个数据内存数据。你可以看源代码。 Bitmap canvas.drawBitmap(source, srcR, dstR, paint);
灰色流连 2014-12-04
  • 打赏
  • 举报
回复
我回收的是第一个bitmap imageview里面用的是第二个bitmap 不是同一个呀
Birds2018 2014-12-04
  • 打赏
  • 举报
回复
要先判断这个bitmap 是否回收过了。bm.recyle(); 你都回收了,就不能绘制在UI上面了。ImageView需要bitmap的数据内容
灰色流连 2014-12-04
  • 打赏
  • 举报
回复
引用 5 楼 birdsaction 的回复:
canvas.drawBitmap(source, srcR, dstR, paint); 这个source就是你传入的Bitmap对象,如果你回收了 系统就会报错。
这个我知道, 我想知道的是newBm我没有手动回收它, 为什么变成回收状态了 也就是说: Bitmap newBm = Bitmap.createScaledBitmap(bm , 100,100, true); 我把bm回收 为什么newBm也跟着回收了?
Birds2018 2014-12-04
  • 打赏
  • 举报
回复
canvas.drawBitmap(source, srcR, dstR, paint); 这个source就是你传入的Bitmap对象,如果你回收了 系统就会报错。
灰色流连 2014-12-04
  • 打赏
  • 举报
回复
引用 3 楼 birdsaction 的回复:
他需要bitmap对象绘制到UI上,这个对象你回收了 就无法绘制了,其实是一个数据内存数据。你可以看源代码。 Bitmap canvas.drawBitmap(source, srcR, dstR, paint);
Bitmap bm = BitmapFactory.decodeResource(res, id); Bitmap newBm = Bitmap.createScaledBitmap(bm , 100,100, true); 我回收的是bm 为何newBm也变成recyled了, 这两个bitmap不是同一个对象啊

80,356

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧