博客
关于我
Android Bitmap操作问题之Canvas: trying to use a recycled bitmap
阅读量:447 次
发布时间:2019-03-06

本文共 834 字,大约阅读时间需要 2 分钟。

Bitmap.recycle 方法被弃用

在Android系统中,Bitmap的存储分为两部分:一部分是Bitmap的数据,另一部分是Bitmap的引用。在Android 2.3之前,Bitmap的引用存放在堆中,而数据部分则存放在栈中。因此,开发者需要主动调用Bitmap.recycle()方法来释放内存。然而,随着Android 2.3及之后版本的更新,情况有所变化。

在Android 2.3之后,Bitmap的数据和引用都存放在堆中。这意味着不再需要手动调用recycle()方法,系统会将不再使用的Bitmap内存交给垃圾回收机制(GC)来处理。因此,recycle()方法被废弃了。

注:系统建议不要手动调用recycle()方法,而是让GC自动处理不再使用的Bitmap。然而,实际使用中发现,直接调用recycle()可能会引发错误,错误信息提示:“Canvas: trying to use a recycled bitmap android.graphics.Bitmap@1a50ff6b”。

这可能是由于在调用recycle()后,再次使用同一个Bitmap对象而引起的。根据Android系统源码的一段注释:“新生成的Bitmap可能与源Bitmap对象相同,也可能已经生成了新的副本。”因此,仍有可能创建同一个对象,导致错误。

问题分析与解决方法

从错误日志可以看出,调用recycle()后,Bitmap的某些方法又被调用了。这可能是因为在调用recycle()后,仍然使用了同一个Bitmap对象。然而,根据系统的注释,新生成的Bitmap可能与源Bitmap对象相同,也可能已经生成了新的副本。因此,仍有可能存在这种情况。

对于不需要兼容Android 2.3及之前版本的情况,建议直接避免使用recycle()方法,让GC自动回收Bitmap内存。这样可以避免因手动调用recycle()而引发的问题,同时也能提高内存管理的效率。

转载地址:http://gidfz.baihongyu.com/

你可能感兴趣的文章
nio 中channel和buffer的基本使用
查看>>
NISP一级,NISP二级报考说明,零基础入门到精通,收藏这篇就够了
查看>>
NI笔试——大数加法
查看>>
NLP 基于kashgari和BERT实现中文命名实体识别(NER)
查看>>
Nmap扫描教程之Nmap基础知识
查看>>
Nmap端口扫描工具Windows安装和命令大全(非常详细)零基础入门到精通,收藏这篇就够了
查看>>
NMAP网络扫描工具的安装与使用
查看>>
NN&DL4.1 Deep L-layer neural network简介
查看>>
NN&DL4.3 Getting your matrix dimensions right
查看>>
NN&DL4.8 What does this have to do with the brain?
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>