我在使用 Android Compose 中的“图像”组件时遇到反复出现的问题。我正在使用 PainterResource 函数将图像加载到“Image”组件中。但是,我经常面临与 java.lang.OutOfMemoryError 相关的崩溃。
我怀疑崩溃是由于与图像加载相关的内存消耗而发生的。我想知道在 Android Compose 中使用“Image”组件时避免这些 OutOfMemoryError 崩溃的最佳实践和技术。任何有关内存高效图像加载策略或其他相关优化的见解将不胜感激。
感谢您提前提供的帮助!
这是其中 1 次崩溃:
Fatal Exception: java.lang.OutOfMemoryError
Failed to allocate a 18195852 byte allocation with 10423024 free bytes and 9MB until OOM
dalvik.system.VMRuntime.newNonMovableArray (VMRuntime.java)
android.graphics.BitmapFactory.nativeDecodeAsset (BitmapFactory.java)
android.graphics.BitmapFactory.decodeStream (BitmapFactory.java:624)
android.graphics.BitmapFactory.decodeResourceStream (BitmapFactory.java:457)
android.graphics.drawable.Drawable.createFromResourceStream (Drawable.java:1152)
android.content.res.ResourcesImpl.createFromResourceStream (ResourcesImpl.java:1296)
android.content.res.ResourcesImpl.loadDrawableForCookie (ResourcesImpl.java:743)
android.content.res.ResourcesImpl.loadDrawable (ResourcesImpl.java:585)
android.content.res.MiuiResourcesImpl.loadDrawable (MiuiResourcesImpl.java:328)
android.content.res.Resources.getDrawable (Resources.java:785)
androidx.compose.ui.res.ImageResources_androidKt.imageResource (ImageResources.android.kt:39)
androidx.compose.ui.res.PainterResources_androidKt.loadImageBitmapResource (PainterResources.android.kt:109)
androidx.compose.ui.res.PainterResources_androidKt.access$loadImageBitmapResource (PainterResources.android.kt:1)
androidx.compose.ui.res.PainterResources_androidKt.painterResource (PainterResources.android.kt:70)
com.ui.screens.main.MainScreenKt$VipLevels$$inlined$ConstraintLayout$5.invoke (ConstraintLayout.kt:2308)
androidx.constraintlayout.compose.ConstraintLayoutKt$ConstraintLayout$2.invoke (ConstraintLayout.kt:470)
com.ui.screens.main.MainScreenKt$VipLevels$$inlined$ConstraintLayout$5.invoke (ConstraintLayout.kt:470)
androidx.compose.runtime.internal.ComposableLambdaImpl.invoke (ComposableLambda.jvm.kt:108)
androidx.compose.runtime.internal.ComposableLambdaImpl.invoke (ComposableLambda.jvm.kt:35)
androidx.compose.runtime.ComposablesKt.ReusableComposeNode (Composables.kt:373)
androidx.compose.ui.layout.LayoutKt.MultiMeasureLayout (Layout.kt:251)
androidx.constraintlayout.compose.ConstraintLayoutKt.ConstraintLayout (ConstraintLayout.kt:467)
com.ui.screens.main.MainScreenKt.VipLevels (MainScreen.kt:314)
com.ui.screens.main.MainScreenKt$MainScreen$$inlined$ConstraintLayout$5.invoke (ConstraintLayout.kt:2394)
androidx.constraintlayout.compose.ConstraintLayoutKt$ConstraintLayout$2.invoke (ConstraintLayout.kt:470)
com.ui.screens.main.MainScreenKt$MainScreen$$inlined$ConstraintLayout$5.invoke (ConstraintLayout.kt:470)
androidx.compose.runtime.internal.ComposableLambdaImpl.invoke (ComposableLambda.jvm.kt:108)
androidx.compose.runtime.internal.ComposableLambdaImpl.invoke (ComposableLambda.jvm.kt:35)
androidx.compose.runtime.ComposablesKt.ReusableComposeNode (Composables.kt:373)
androidx.compose.ui.layout.LayoutKt.MultiMeasureLayout (Layout.kt:251)
androidx.constraintlayout.compose.ConstraintLayoutKt.ConstraintLayout (ConstraintLayout.kt:467)
com.ui.screens.main.MainScreenKt.MainScreen (MainScreen.kt:127)
com.ui.screens.main.MainScreenRoute.Content (MainScreen.kt:69)
com.navigation.NavRoute$composable$1.invoke (NavRoute.kt:100)
com.navigation.NavRoute$composable$1.access$invoke$lambda$0 (NavRoute.kt:59)
com.navigation.NavRoute$composable$1.invoke (NavRoute.kt:59)
androidx.compose.runtime.internal.ComposableLambdaImpl.invoke (ComposableLambda.jvm.kt:117)
androidx.compose.runtime.internal.ComposableLambdaImpl.invoke (ComposableLambda.jvm.kt:35)
androidx.navigation.compose.NavHostKt$NavHost$4$2.invoke (NavHost.kt:173)
androidx.navigation.compose.NavHostKt$NavHost$4$2.invoke (NavHost.kt:172)
androidx.compose.runtime.internal.ComposableLambdaImpl.invoke (ComposableLambda.jvm.kt:108)
androidx.compose.runtime.internal.ComposableLambdaImpl.invoke (ComposableLambda.jvm.kt:35)
androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider (CompositionLocal.kt:248)
androidx.compose.runtime.saveable.SaveableStateHolderImpl.SaveableStateProvider (SaveableStateHolder.kt:84)
androidx.navigation.compose.NavBackStackEntryProviderKt.SaveableStateProvider (NavBackStackEntryProvider.kt:65)
androidx.navigation.compose.NavBackStackEntryProviderKt.access$SaveableStateProvider (NavBackStackEntryProvider.kt:1)
androidx.navigation.compose.NavBackStackEntryProviderKt$LocalOwnersProvider$1.invoke (NavBackStackEntryProvider.kt:52)
androidx.navigation.compose.NavBackStackEntryProviderKt$LocalOwnersProvider$1.invoke (NavBackStackEntryProvider.kt:51)
androidx.compose.runtime.internal.ComposableLambdaImpl.invoke (ComposableLambda.jvm.kt:108)
androidx.compose.runtime.internal.ComposableLambdaImpl.invoke (ComposableLambda.jvm.kt:35)
androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider (CompositionLocal.kt:228)
androidx.navigation.compose.NavBackStackEntryProviderKt.LocalOwnersProvider (NavBackStackEntryProvider.kt:47)
androidx.navigation.compose.NavHostKt$NavHost$4.invoke (NavHost.kt:172)
androidx.navigation.compose.NavHostKt$NavHost$4.invoke (NavHost.kt:146)
androidx.compose.runtime.internal.ComposableLambdaImpl.invoke (ComposableLambda.jvm.kt:117)
androidx.compose.runtime.internal.ComposableLambdaImpl.invoke (ComposableLambda.jvm.kt:35)
androidx.compose.animation.CrossfadeKt$Crossfade$5$1.invoke (Crossfade.kt:133)
androidx.compose.animation.CrossfadeKt$Crossfade$5$1.access$invoke$lambda$1 (Crossfade.kt:128)
androidx.compose.animation.CrossfadeKt$Crossfade$5$1.invoke (Crossfade.kt:128)
androidx.compose.runtime.internal.ComposableLambdaImpl.invoke (ComposableLambda.jvm.kt:108)
androidx.compose.runtime.internal.ComposableLambdaImpl.invoke (ComposableLambda.jvm.kt:35)
androidx.compose.animation.CrossfadeKt.Crossfade (Crossfade.kt:142)
androidx.compose.animation.CrossfadeKt.Crossfade (Crossfade.kt:73)
androidx.navigation.compose.NavHostKt.NavHost (NavHost.kt:146)
androidx.navigation.compose.NavHostKt$NavHost$5.invoke (NavHost.kt:2)
androidx.navigation.compose.NavHostKt$NavHost$5.invoke (NavHost.kt:1)
androidx.compose.runtime.RecomposeScopeImpl.compose (RecomposeScopeImpl.kt:192)
androidx.compose.runtime.ComposerImpl.recomposeToGroupEnd (Composer.kt:2511)
androidx.compose.runtime.ComposerImpl.skipCurrentGroup (Composer.kt:2780)
androidx.compose.runtime.ComposerImpl.doCompose (Composer.kt:3259)
androidx.compose.runtime.ComposerImpl.recompose$runtime_release (Composer.kt:3210)
androidx.compose.runtime.CompositionImpl.recompose (Composition.kt:864)
androidx.compose.runtime.Recomposer.performRecompose (Recomposer.kt:1125)
androidx.compose.runtime.Recomposer.access$setCloseCause$p (Recomposer.kt:124)
androidx.compose.runtime.Recomposer.access$performRecompose (Recomposer.kt:124)
androidx.compose.runtime.Recomposer$runRecomposeAndApplyChanges$2$1.invoke (Recomposer.kt:580)
androidx.compose.runtime.Recomposer$runRecomposeAndApplyChanges$2$1.invoke (Recomposer.kt:548)
androidx.compose.ui.platform.AndroidUiFrameClock$withFrameNanos$2$callback$1.doFrame (AndroidUiFrameClock.android.kt:41)
androidx.compose.ui.platform.AndroidUiDispatcher.performFrameDispatch (AndroidUiDispatcher.android.kt:109)
androidx.compose.ui.platform.AndroidUiDispatcher.access$setScheduledFrameDispatch$p (AndroidUiDispatcher.android.kt:41)
androidx.compose.ui.platform.AndroidUiDispatcher.access$performFrameDispatch (AndroidUiDispatcher.android.kt:41)
androidx.compose.ui.platform.AndroidUiDispatcher$dispatchCallback$1.doFrame (AndroidUiDispatcher.android.kt:69)
android.view.Choreographer$CallbackRecord.run (Choreographer.java:871)
android.view.Choreographer.doCallbacks (Choreographer.java:685)
android.view.Choreographer.doFrame (Choreographer.java:618)
android.view.Choreographer$FrameDisplayEventReceiver.run (Choreographer.java:859)
android.os.Handler.handleCallback (Handler.java:754)
android.os.Handler.dispatchMessage (Handler.java:95)
android.os.Looper.loop (Looper.java:165)
android.app.ActivityThread.main (ActivityThread.java:6375)
java.lang.reflect.Method.invoke (Method.java)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:912)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:802)
已解决
我对所有图像使用一种尺寸。我将所有图像从“drawable”文件夹移动到“drawable-nodpi”文件夹。系统可能正在尝试缩放图像以适应各种屏幕尺寸,通过这样做,由于图像尺寸较大,在某些设备上不再出现该问题。