leavesCZY / Matisse

A Image/Video Selector Implemented with Jetpack Compose(一个用 Jetpack Compose 实现的 Android 图片视频选择框架)
https://github.com/leavesCZY/Matisse/wiki
Apache License 2.0
773 stars 60 forks source link

java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@d00a93f #51

Closed limingbang closed 7 months ago

limingbang commented 7 months ago

机型:魅族m15 Android 7.1 复现:第一次跳转Matisse选择图片后,跳转到目标页,接着退出当前页回到首页再次跳转Matisse compose:composeBom = "2024.03.00"(1.6.4)尝试过1.5.4 coil:2.5.0 github-matisse = "2.0.0"


java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@f5cd1e3 at android.graphics.Canvas.throwIfCannotDraw(Canvas.java:1464) at android.view.DisplayListCanvas.throwIfCannotDraw(DisplayListCanvas.java:259) at android.graphics.Canvas.drawBitmap(Canvas.java:1672) at androidx.compose.ui.graphics.AndroidCanvas.drawImageRect-HPBpro0(AndroidCanvas.android.kt:275) at androidx.compose.ui.graphics.drawscope.CanvasDrawScope.drawImage-AZ2fEMs(CanvasDrawScope.kt:256) at androidx.compose.ui.node.LayoutNodeDrawScope.drawImage-AZ2fEMs(LayoutNodeDrawScope.kt) at androidx.compose.ui.graphics.drawscope.DrawScope.drawImage-AZ2fEMs$default(DrawScope.kt:551) at androidx.compose.ui.graphics.painter.BitmapPainter.onDraw(BitmapPainter.kt:93) at androidx.compose.ui.graphics.painter.Painter.draw-x_KDEd0(Painter.kt:212) at coil.compose.AsyncImagePainter.onDraw(AsyncImagePainter.kt:208) at androidx.compose.ui.graphics.painter.Painter.draw-x_KDEd0(Painter.kt:212) at coil.compose.ContentPainterModifier.draw(ContentPainterModifier.kt:197) at androidx.compose.ui.node.BackwardsCompatNode.draw(BackwardsCompatNode.kt:349) at androidx.compose.ui.node.LayoutNodeDrawScope.drawDirect-x_KDEd0$ui_release(LayoutNodeDrawScope.kt:105) at androidx.compose.ui.node.LayoutNodeDrawScope.draw-x_KDEd0$ui_release(LayoutNodeDrawScope.kt:86) at androidx.compose.ui.node.NodeCoordinator.drawContainedDrawModifiers(NodeCoordinator.kt:364) at androidx.compose.ui.node.NodeCoordinator.draw(NodeCoordinator.kt:353) at androidx.compose.ui.node.LayoutModifierNodeCoordinator.performDraw(LayoutModifierNodeCoordinator.kt:176) at androidx.compose.ui.node.NodeCoordinator.drawContainedDrawModifiers(NodeCoordinator.kt:361) at androidx.compose.ui.node.NodeCoordinator.access$drawContainedDrawModifiers(NodeCoordinator.kt:54) at androidx.compose.ui.node.NodeCoordinator$drawBlock$1$1.invoke(NodeCoordinator.kt:383) at androidx.compose.ui.node.NodeCoordinator$drawBlock$1$1.invoke(NodeCoordinator.kt:382) at androidx.compose.runtime.snapshots.Snapshot$Companion.observe(Snapshot.kt:2303) at androidx.compose.runtime.snapshots.SnapshotStateObserver$ObservedScopeMap.observe(SnapshotStateObserver.kt:500) at androidx.compose.runtime.snapshots.SnapshotStateObserver.observeReads(SnapshotStateObserver.kt:256) at androidx.compose.ui.node.OwnerSnapshotObserver.observeReads$ui_release(OwnerSnapshotObserver.kt:133) at androidx.compose.ui.node.NodeCoordinator$drawBlock$1.invoke(NodeCoordinator.kt:382) at androidx.compose.ui.node.NodeCoordinator$drawBlock$1.invoke(NodeCoordinator.kt:380) at androidx.compose.ui.platform.RenderNodeApi23.record(RenderNodeApi23.android.kt:280) at androidx.compose.ui.platform.RenderNodeLayer.updateDisplayList(RenderNodeLayer.android.kt:335) at androidx.compose.ui.platform.RenderNodeLayer.drawLayer(RenderNodeLayer.android.kt:277) at androidx.compose.ui.node.NodeCoordinator.draw(NodeCoordinator.kt:348) at androidx.compose.ui.node.LayoutModifierNodeCoordinator.performDraw(LayoutModifierNodeCoordinator.kt:176) at androidx.compose.ui.node.NodeCoordinator.drawContainedDrawModifiers(NodeCoordinator.kt:361) at androidx.compose.ui.node.NodeCoordinator.draw(NodeCoordinator.kt:353) at androidx.compose.ui.node.LayoutModifierNodeCoordinator.performDraw(LayoutModifierNodeCoordinator.kt:176) at androidx.compose.ui.node.NodeCoordinator.drawContainedDrawModifiers(NodeCoordinator.kt:361) at androidx.compose.ui.node.NodeCoordinator.draw(NodeCoordinator.kt:353) at androidx.compose.ui.node.LayoutModifierNodeCoordinator.performDraw(LayoutModifierNodeCoordinator.kt:176) at androidx.compose.ui.node.NodeCoordinator.drawContainedDrawModifiers(NodeCoordinator.kt:361) at androidx.compose.ui.node.NodeCoordinator.draw(NodeCoordinator.kt:353) at androidx.compose.ui.node.LayoutNode.draw$ui_release(LayoutNode.kt:926) at androidx.compose.ui.node.InnerNodeCoordinator.performDraw(InnerNodeCoordinator.kt:174) 2024-04-03 16:14:55.314 19678-19678 AndroidRuntime com.ming.opengl E at androidx.compose.ui.node.LayoutNodeDrawScope.drawContent(LayoutNodeDrawScope.kt:66) at androidx.compose.material.ripple.AndroidRippleIndicationInstance.drawIndication(Ripple.android.kt:172) at androidx.compose.foundation.IndicationModifier.draw(Indication.kt:183) at androidx.compose.ui.node.BackwardsCompatNode.draw(BackwardsCompatNode.kt:349) at androidx.compose.ui.node.LayoutNodeDrawScope.drawDirect-x_KDEd0$ui_release(LayoutNodeDrawScope.kt:105) at androidx.compose.ui.node.LayoutNodeDrawScope.performDraw(LayoutNodeDrawScope.kt:76) at androidx.compose.ui.node.LayoutNodeDrawScope.drawContent(LayoutNodeDrawScope.kt:55) at androidx.compose.foundation.BackgroundNode.draw(Background.kt:159) at androidx.compose.ui.node.LayoutNodeDrawScope.drawDirect-x_KDEd0$ui_release(LayoutNodeDrawScope.kt:105) at androidx.compose.ui.node.LayoutNodeDrawScope.draw-x_KDEd0$ui_release(LayoutNodeDrawScope.kt:86) at androidx.compose.ui.node.NodeCoordinator.drawContainedDrawModifiers(NodeCoordinator.kt:364) at androidx.compose.ui.node.NodeCoordinator.access$drawContainedDrawModifiers(NodeCoordinator.kt:54) at androidx.compose.ui.node.NodeCoordinator$drawBlock$1$1.invoke(NodeCoordinator.kt:383) at androidx.compose.ui.node.NodeCoordinator$drawBlock$1$1.invoke(NodeCoordinator.kt:382) at androidx.compose.runtime.snapshots.Snapshot$Companion.observe(Snapshot.kt:2303) at androidx.compose.runtime.snapshots.SnapshotStateObserver$ObservedScopeMap.observe(SnapshotStateObserver.kt:500) at androidx.compose.runtime.snapshots.SnapshotStateObserver.observeReads(SnapshotStateObserver.kt:256) at androidx.compose.ui.node.OwnerSnapshotObserver.observeReads$ui_release(OwnerSnapshotObserver.kt:133) at androidx.compose.ui.node.NodeCoordinator$drawBlock$1.invoke(NodeCoordinator.kt:382) at androidx.compose.ui.node.NodeCoordinator$drawBlock$1.invoke(NodeCoordinator.kt:380) at androidx.compose.ui.platform.RenderNodeApi23.record(RenderNodeApi23.android.kt:280) at androidx.compose.ui.platform.RenderNodeLayer.updateDisplayList(RenderNodeLayer.android.kt:335) at androidx.compose.ui.platform.RenderNodeLayer.drawLayer(RenderNodeLayer.android.kt:277) at androidx.compose.ui.node.NodeCoordinator.draw(NodeCoordinator.kt:348) at androidx.compose.ui.node.LayoutModifierNodeCoordinator.performDraw(LayoutModifierNodeCoordinator.kt:176) at androidx.compose.ui.node.NodeCoordinator.drawContainedDrawModifiers(NodeCoordinator.kt:361) at androidx.compose.ui.node.NodeCoordinator.draw(NodeCoordinator.kt:353) at androidx.compose.ui.node.LayoutModifierNodeCoordinator.performDraw(LayoutModifierNodeCoordinator.kt:176) at androidx.compose.ui.node.NodeCoordinator.drawContainedDrawModifiers(NodeCoordinator.kt:361) at androidx.compose.ui.node.NodeCoordinator.draw(NodeCoordinator.kt:353) at androidx.compose.ui.node.LayoutModifierNodeCoordinator.performDraw(LayoutModifierNodeCoordinator.kt:176) at androidx.compose.ui.node.NodeCoordinator.drawContainedDrawModifiers(NodeCoordinator.kt:361) at androidx.compose.ui.node.NodeCoordinator.access$drawContainedDrawModifiers(NodeCoordinator.kt:54) at androidx.compose.ui.node.NodeCoordinator$drawBlock$1$1.invoke(NodeCoordinator.kt:383) at androidx.compose.ui.node.NodeCoordinator$drawBlock$1$1.invoke(NodeCoordinator.kt:382) at androidx.compose.runtime.snapshots.Snapshot$Companion.observe(Snapshot.kt:2303) at androidx.compose.runtime.snapshots.SnapshotStateObserver$ObservedScopeMap.observe(SnapshotStateObserver.kt:500) at androidx.compose.runtime.snapshots.SnapshotStateObserver.observeReads(SnapshotStateObserver.kt:256) at androidx.compose.ui.node.OwnerSnapshotObserver.observeReads$ui_release(OwnerSnapshotObserver.kt:133) at androidx.compose.ui.node.NodeCoordinator$drawBlock$1.invoke(NodeCoordinator.kt:382) at androidx.compose.ui.node.NodeCoordinator$drawBlock$1.invoke(NodeCoordinator.kt:380) at androidx.compose.ui.platform.RenderNodeApi23.record(RenderNodeApi23.android.kt:280) 2024-04-03 16:14:55.315 19678-19678 AndroidRuntime com.ming.opengl E at androidx.compose.ui.platform.RenderNodeLayer.updateDisplayList(RenderNodeLayer.android.kt:335) at androidx.compose.ui.platform.RenderNodeLayer.drawLayer(RenderNodeLayer.android.kt:277) at androidx.compose.ui.node.NodeCoordinator.draw(NodeCoordinator.kt:348) at androidx.compose.ui.node.LayoutNode.draw$ui_release(LayoutNode.kt:926) at androidx.compose.ui.node.InnerNodeCoordinator.performDraw(InnerNodeCoordinator.kt:174) at androidx.compose.ui.node.LayoutNodeDrawScope.drawContent(LayoutNodeDrawScope.kt:66) at androidx.compose.foundation.DrawOverscrollModifier.draw(AndroidOverscroll.android.kt:81) at androidx.compose.ui.node.BackwardsCompatNode.draw(BackwardsCompatNode.kt:349) at androidx.compose.ui.node.LayoutNodeDrawScope.drawDirect-x_KDEd0$ui_release(LayoutNodeDrawScope.kt:105) at androidx.compose.ui.node.LayoutNodeDrawScope.draw-x_KDEd0$ui_release(LayoutNodeDrawScope.kt:86) at androidx.compose.ui.node.NodeCoordinator.drawContainedDrawModifiers(NodeCoordinator.kt:364) at androidx.compose.ui.node.NodeCoordinator.access$drawContainedDrawModifiers(NodeCoordinator.kt:54) at androidx.compose.ui.node.NodeCoordinator$drawBlock$1$1.invoke(NodeCoordinator.kt:383) at androidx.compose.ui.node.NodeCoordinator$drawBlock$1$1.invoke(NodeCoordinator.kt:382) at androidx.compose.runtime.snapshots.Snapshot$Companion.observe(Snapshot.kt:2303) at androidx.compose.runtime.snapshots.SnapshotStateObserver$ObservedScopeMap.observe(SnapshotStateObserver.kt:500) at androidx.compose.runtime.snapshots.SnapshotStateObserver.observeReads(SnapshotStateObserver.kt:256) at androidx.compose.ui.node.OwnerSnapshotObserver.observeReads$ui_release(OwnerSnapshotObserver.kt:133) at androidx.compose.ui.node.NodeCoordinator$drawBlock$1.invoke(NodeCoordinator.kt:382) at androidx.compose.ui.node.NodeCoordinator$drawBlock$1.invoke(NodeCoordinator.kt:380) at androidx.compose.ui.platform.RenderNodeApi23.record(RenderNodeApi23.android.kt:280) at androidx.compose.ui.platform.RenderNodeLayer.updateDisplayList(RenderNodeLayer.android.kt:335) at androidx.compose.ui.platform.AndroidComposeView.dispatchDraw(AndroidComposeView.android.kt:1236) at android.view.View.draw(View.java:17338) at android.view.View.updateDisplayListIfDirty(View.java:16312) at android.view.View.draw(View.java:17096) at android.view.ViewGroup.drawChild(ViewGroup.java:3727) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3513) at android.view.View.updateDisplayListIfDirty(View.java:16307) at android.view.View.draw(View.java:17096) at android.view.ViewGroup.drawChild(ViewGroup.java:3727) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3513) at android.view.View.updateDisplayListIfDirty(View.java:16307) at android.view.View.draw(View.java:17096) at android.view.ViewGroup.drawChild(ViewGroup.java:3727) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3513) at android.view.View.updateDisplayListIfDirty(View.java:16307) at android.view.View.draw(View.java:17096) at android.view.ViewGroup.drawChild(ViewGroup.java:3727) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3513) at android.view.View.updateDisplayListIfDirty(View.java:16307) at android.view.View.draw(View.java:17096) at android.view.ViewGroup.drawChild(ViewGroup.java:3727) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3513) at android.view.View.updateDisplayListIfDirty(View.java:16307) at android.view.View.draw(View.java:17096) at android.view.ViewGroup.drawChild(ViewGroup.java:3727) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3513) at android.view.View.draw(View.java:17338) at com.android.internal.policy.DecorView.draw(DecorView.java:779) at android.view.View.updateDisplayListIfDirty(View.java:16312) at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:661) at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:667) at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:775) 2024-04-03 16:14:55.315 19678-19678 AndroidRuntime com.ming.opengl E at android.view.ViewRootImpl.draw(ViewRootImpl.java:2957) at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2765) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2351) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1383) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6592) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:966) at android.view.Choreographer.doCallbacks(Choreographer.java:778) at android.view.Choreographer.doFrame(Choreographer.java:692) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:952) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6466) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1003) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:893)

leavesCZY commented 7 months ago

这种问题倒是第一次见到。你是只在这台机型上发现这个问题吗?是属于偶发还是会固定出现呢?异常信息是说已回收的 bitmap 被重复使用了,而 Matisse 内部也并不会去更改 Coil 的默认配置,这个是否可能是由于你的项目对 Coil 做了一些自定义配置导致的

limingbang commented 7 months ago

和这个库没啥关系,可以关掉了。应该是Coil还缓存了被recycle的bitmap。 在目标页回收了bitmap然后退出,再重新拉起Matisse

LaunchedEffect(Unit) {
        context.imageLoader
            .enqueue(
                  ImageRequest.Builder(context)
                 .data(imageUri)
                 .scale(Scale.FIT)
                 .allowHardware(false)
                 .listener(onSuccess = { _, result ->
                         imageBitmap = result.drawable.toBitmap()
                 })
                .build()
           )

       delay(3000)
       imageBitmap?.recycle()
}
leavesCZY commented 7 months ago

ok