coil-kt / coil

Image loading for Android and Compose Multiplatform.
https://coil-kt.github.io/coil/
Apache License 2.0
10.84k stars 665 forks source link

Coil access disk on main thread? #2467

Closed mahmed1987 closed 2 months ago

mahmed1987 commented 2 months ago

Describe the bug Why does coil access the disk on main thread when displaying a picture via a fileUri ? I have strictmode enabled and I found it out via the logs.

I just take a uri of a taken picture and display it via AsyncImage like this

AsyncImage( model = ImageRequest.Builder(LocalContext.current) .data(fileUri) .crossfade(true) .build())

Logs/Screenshots These are the logs of the strictmode

StrictMode policy violation; ~duration=7 ms: android.os.strictmode.DiskReadViolation (Ask Gemini) at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1658) at java.io.UnixFileSystem.getLastModifiedTime(UnixFileSystem.java:321) at java.io.File.lastModified(File.java:937) at coil.key.FileKeyer.key(FileKeyer.kt:10) at coil.key.FileKeyer.key(FileKeyer.kt:6) at coil.ComponentRegistry.key(ComponentRegistry.kt:54) at coil.memory.MemoryCacheService.newCacheKey(MemoryCacheService.kt:48) at coil.intercept.EngineInterceptor.intercept(EngineInterceptor.kt:64) at coil.intercept.RealInterceptorChain.proceed(RealInterceptorChain.kt:25) at coil.RealImageLoader$executeMain$result$1.invokeSuspend(RealImageLoader.kt:191) at coil.RealImageLoader$executeMain$result$1.invoke(Unknown Source:8) at coil.RealImageLoader$executeMain$result$1.invoke(Unknown Source:4) at kotlinx.coroutines.intrinsics.UndispatchedKt.startUndispatchedOrReturn(Undispatched.kt:78) at kotlinx.coroutines.BuildersKt__Builders_commonKt.withContext(Builders.common.kt:167) at kotlinx.coroutines.BuildersKt.withContext(Unknown Source:1) at coil.RealImageLoader.executeMain(RealImageLoader.kt:182) at coil.RealImageLoader.access$executeMain(RealImageLoader.kt:65) at coil.RealImageLoader$executeMain$1.invokeSuspend(Unknown Source:16) at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) at kotlinx.coroutines.DispatchedTaskKt.resume(DispatchedTask.kt:235) at kotlinx.coroutines.DispatchedTaskKt.resumeUnconfined(DispatchedTask.kt:191) at kotlinx.coroutines.DispatchedTaskKt.dispatch(DispatchedTask.kt:163) at kotlinx.coroutines.CancellableContinuationImpl.dispatchResume(CancellableContinuationImpl.kt:474) at kotlinx.coroutines.CancellableContinuationImpl.resumeImpl(CancellableContinuationImpl.kt:508) at kotlinx.coroutines.CancellableContinuationImpl.resumeImpl$default(CancellableContinuationImpl.kt:497) at kotlinx.coroutines.CancellableContinuationImpl.resumeWith(CancellableContinuationImpl.kt:368) at kotlinx.coroutines.flow.StateFlowSlot.makePending(StateFlow.kt:284) at kotlinx.coroutines.flow.StateFlowImpl.updateState(StateFlow.kt:349) at kotlinx.coroutines.flow.StateFlowImpl.setValue(StateFlow.kt:316) at coil.compose.ConstraintsSizeResolver.measure-3p2s80s(AsyncImage.kt:209) at androidx.compose.ui.node.BackwardsCompatNode.measure-3p2s80s(BackwardsCompatNode.kt:312) at androidx.compose.ui.node.LayoutModifierNodeCoordinator.measure-BRTryo0(LayoutModifierNodeCoordinator.kt:188)

Version I am using coil version 2.4.0

colinrtwhite commented 2 months ago

This is default behaviour that's tough to change without breaking existing users (though I'm thinking about changing it in 3.x). The behaviour can be disabled/enabled using this API.