今天遇到Canvas: trying to use a recycled bitmap android.graphics.Bitmap问题

来源:互联网 发布:fx3u 48m编程手册中文 编辑:程序博客网 时间:2024/05/17 06:55

今天遇到Canvas: trying to use a recycled bitmap android.graphics.Bitmap问题

我这用到bitmap中间变量了,还用到

Bitmap bitmap = Bitmap.createBitmap(temp, 0, 0, width, height, matrix,true);方法了。

把temp.recycle();后就报了那个错误。

 

原因是传入的temp图片和新的bitmap图片分辨率一样。createBitmap的源码就会直接返回temp对象,而不是新建一个bitmap对象,这样temp.recycle()被回收后,bitmap自然也被回收了。

解决方法把传入图片换个分辨率。

或者使 width, height, 和生成matrix的width, height不一样,就行了

 

if(newHeight==height&&newWidth==width){

//强制使新旧图片分辨率不一样,防止temp和bitmap是同一对象

height=height-1;

}

原创粉丝点击