尝试显示位图时一目了然的应用程序小部件图像崩溃

Crash in glance app widget image when trying to display bitmap

当我尝试在应用程序中显示位图图像(我的应用程序图标)时,它工作正常,但当我尝试在 Widget Glance 中显示它时崩溃。

这是我的位图代码:

    val icon = packageManager.getApplicationIcon("com.myapp.packagename")
    val bitmap: Bitmap = try {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            getAppIconV26(applicationContext, "com.myapp.packagename")
        } else {
            (icon as BitmapDrawable).bitmap
        }
    } catch (e: Exception) {
        (ResourcesCompat.getDrawable(applicationContext.resources, R.drawable.placeholder, null) as BitmapDrawable).bitmap

这是 jetpack compose 图像,可以很好地加载位图:

androidx.compose.foundation.Image(
    modifier = Modifier.size(46.dp), 
    bitmap = bitmap.asImageBitmap(),
    contentDescription = null),
}

但是当我尝试使用 Jetpack Glance Image 在小部件中加载相同的位图时,小部件崩溃了:

androidx.glance.Image(
    modifier = GlanceModifier.size(46.dp),
    provider = ImageProvider(bitmap),
    contentDescription = null,
)

崩溃:

E/GlanceAppWidget: Error in Glance App Widget
    java.lang.RuntimeException: Tried to marshall a Parcel that contained Binder objects.
        at android.os.Parcel.nativeMarshall(Native Method)
        at android.os.Parcel.marshall(Parcel.java:620)
        at androidx.core.widget.RemoteViewsCompatService$RemoteViewsCompatServiceData$Companion.serializeToBytes$core_remoteviews_release(RemoteViewsCompatService.kt:245)
        at androidx.core.widget.RemoteViewsCompatService$RemoteViewsCompatServiceData$Companion.create(RemoteViewsCompatService.kt:166)
        at androidx.core.widget.RemoteViewsCompatService$Companion.saveItems(RemoteViewsCompatService.kt:306)
        at androidx.core.widget.RemoteViewsCompat.setRemoteAdapter(RemoteViewsCompat.kt:86)
        at androidx.glance.appwidget.translators.LazyListTranslatorKt.translateEmittableLazyList(LazyListTranslator.kt:90)
        at androidx.glance.appwidget.translators.LazyListTranslatorKt.translateEmittableLazyColumn(LazyListTranslator.kt:45)
        at androidx.glance.appwidget.RemoteViewsTranslatorKt.translateChild(RemoteViewsTranslator.kt:143)
        at androidx.glance.appwidget.RemoteViewsTranslatorKt.setChildren(RemoteViewsTranslator.kt:335)
        at androidx.glance.appwidget.RemoteViewsTranslatorKt.translateEmittableColumn(RemoteViewsTranslator.kt:268)
        at androidx.glance.appwidget.RemoteViewsTranslatorKt.translateChild(RemoteViewsTranslator.kt:140)
        at androidx.glance.appwidget.RemoteViewsTranslatorKt.translateComposition(RemoteViewsTranslator.kt:96)
        at androidx.glance.appwidget.RemoteViewsTranslatorKt.translateComposition-mU3eQPI(RemoteViewsTranslator.kt:63)
        at androidx.glance.appwidget.GlanceAppWidget$composeForSize.invokeSuspend(GlanceAppWidget.kt:393)
        at androidx.glance.appwidget.GlanceAppWidget$composeForSize.invoke(Unknown Source:8)
        at androidx.glance.appwidget.GlanceAppWidget$composeForSize.invoke(Unknown Source:4)
        at kotlinx.coroutines.intrinsics.UndispatchedKt.startUndispatchedOrReturn(Undispatched.kt:89)
        at kotlinx.coroutines.BuildersKt__Builders_commonKt.withContext(Builders.common.kt:165)
        at kotlinx.coroutines.BuildersKt.withContext(Unknown Source:1)
        at androidx.glance.appwidget.GlanceAppWidget.composeForSize-AAqiGWc$glance_appwidget_release(GlanceAppWidget.kt:371)
        at androidx.glance.appwidget.GlanceAppWidget.compose$glance_appwidget_release(GlanceAppWidget.kt:218)
        at androidx.glance.appwidget.GlanceAppWidget.compose$glance_appwidget_release(GlanceAppWidget.kt:201)
        at androidx.glance.appwidget.GlanceAppWidget$compose.invokeSuspend(Unknown Source:19)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
        at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571)
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750)
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678)
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665)

我不明白这次崩溃。有人可以帮助我了解问题所在吗?

您可以使用 val context = LocalContext.current 代替 applicationContext 吗?此外,请使用 BitmapImageProvider 而不是 ImageProvider.

Glance 转化为实际的 RemoteView。然后将它们传递给主机进程(即 launcher/homescreen)。可以在进程之间传递的对象的大小有限制。

您似乎正在使用显示许多位图的 LazyColumn(因此是 ListView)。可能是您达到了大小限制。

有几件事要尝试:

  • 尝试只显示一个具有相同代码的
    • 有效吗?
    • 再传一个小Bitmap可以吗?也许问题出在位图大小或类型上?
  • 或者尝试使用 URI 而不是位图或限制要显示的项目数。