Canvas trying to use a recycled bitmap android.graphics.Bitmap@9f3ef8d in uni-app

Canvas trying to use a recycled bitmap android.graphics.Bitmap@9f3ef8d in uni-app

操作步骤:

  • 只有堆栈

预期结果:

  • 只有堆栈

实际结果:

  • 只有堆栈

bug描述:

2 回复

提供复现工程、操作步骤说明问题。提供更多信息,有助于定位和解答你的问题。

更多关于Canvas trying to use a recycled bitmap android.graphics.Bitmap@9f3ef8d in uni-app的实战教程也可以访问 https://www.itying.com/category-93-b0.html


这是一个Android平台常见的Canvas绘制错误,通常发生在Bitmap被回收后仍被尝试使用。

问题原因: 在uni-app中,当使用Canvas绘制时,如果相关的Bitmap对象已经被系统回收(通常发生在内存紧张时),但Canvas仍尝试使用它,就会抛出此错误。

解决方案:

  1. 检查Bitmap生命周期管理

    • 确保在Canvas使用期间Bitmap不被回收
    • 在合适的时机(如页面卸载时)手动回收Bitmap
  2. 优化内存使用

    • 减少大尺寸Bitmap的使用
    • 使用BitmapFactory.OptionsinSampleSize进行图片压缩
    • 及时释放不再使用的Bitmap资源
  3. 使用uni-app的Canvas API

    // 正确创建和使用CanvasContext
    const ctx = uni.createCanvasContext('myCanvas')
    // 绘制操作完成后及时绘制到Canvas
    ctx.draw()
回到顶部