coil-kt / coil

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

Crash when trying to load image in Android emulator #1103

Closed lukaszkalnik closed 2 years ago

lukaszkalnik commented 2 years ago

I use coil-compose and try to load an image in my composable function:

            Image(
                painter = rememberImagePainter("https://image.tmdb.org/t/p/w780/sjx6zjQI2dLGtEL0HGWsnq6UyLU.jpg"),
                contentDescription = null,
                modifier = Modifier.height(120.dp),
            )

However the app crashes on Android emulator with the following exception:

    java.nio.channels.UnresolvedAddressException
        at sun.nio.ch.Net.checkAddress(Net.java:107)
        at sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:663)
        at io.ktor.network.sockets.SocketImpl.connect$ktor_network(SocketImpl.kt:32)
        at io.ktor.network.sockets.ConnectUtilsJvmKt.connect(ConnectUtilsJvm.kt:19)
        at io.ktor.network.sockets.TcpSocketBuilder.connect(TcpSocketBuilder.kt:38)
        at io.ktor.client.engine.cio.ConnectionFactory.connect(ConnectionFactory.kt:24)
        at io.ktor.client.engine.cio.Endpoint$connect$2$connect$1.invokeSuspend(Endpoint.kt:185)
        at io.ktor.client.engine.cio.Endpoint$connect$2$connect$1.invoke(Unknown Source:8)
        at io.ktor.client.engine.cio.Endpoint$connect$2$connect$1.invoke(Unknown Source:4)
        at kotlinx.coroutines.intrinsics.UndispatchedKt.startUndispatchedOrReturnIgnoreTimeout(Undispatched.kt:100)
        at kotlinx.coroutines.TimeoutKt.setupTimeout(Timeout.kt:148)
        at kotlinx.coroutines.TimeoutKt.withTimeoutOrNull(Timeout.kt:104)
        at io.ktor.client.engine.cio.Endpoint.connect(Endpoint.kt:193)
        at io.ktor.client.engine.cio.Endpoint.access$connect(Endpoint.kt:28)
        at io.ktor.client.engine.cio.Endpoint$makeDedicatedRequest$1.invokeSuspend(Endpoint.kt:105)
        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)

Coil 1.4.0 Android emulator Android version S

This code works correctly on an Android 9 device.

colinrtwhite commented 2 years ago

The exception is thrown by Ktor which Coil doesn't use. It's unrelated to Coil, but you may need to set a CoroutineExceptionHander for where you launch the Ktor network request to catch the exception.

lukaszkalnik commented 2 years ago

Ok, thank you for the diagnosis nevertheless and sorry for bothering you!

panpf commented 1 year ago

Ok, thank you for the diagnosis nevertheless and sorry for bothering you!

Hello, can you tell me how you solved this problem later?

lukaszkalnik commented 1 year ago

I think the emulator had no network or something like this. It was indeed not related to Coil.

panpf commented 1 year ago

I think the emulator had no network or something like this. It was indeed not related to Coil.

But this exception shouldn't cause the app to crash, because Coil used try-catch to try to catch all the exceptions, but apparently it didn't, and now I'm suffering from this issue.

Have you left this problem alone? Or is there some way for Coil's try-catch to catch this exception?