Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jetpack Compose Coil-AsyncImage: Howto refresh/reload the image when the image URI does not change

I use AsyncImage to load an image from a remote location. Use case is a profile picture which is taken from the cam then uploaded to a remote database and then it shall be refreshed in the UI.

Now it can happen that the image changes but the URI remains the same. I then trigger a recomposition, but the image remains the same.

I see nothing happening in the loading/success/error callbacks. I tried to use an ImageRequest as model instead of the URI and tried to disable all caches there but this resulted in an error.

When i try another URI instead the Image changes.

So question here is how to make the AsyncImage refresh also if the URI does not change

like image 613
Romain Scherfflein Avatar asked Dec 01 '25 16:12

Romain Scherfflein


1 Answers

As of Coil V3, there is the AsyncImagePainer.reload() functionality.
You can try to use it as follows:

val sizeResolver = rememberConstraintsSizeResolver()
val painter = rememberAsyncImagePainter(
    model = ImageRequest.Builder(LocalPlatformContext.current)
        .data("https://example.com/image.jpg")
        .size(sizeResolver)
        .build(),
)

Button(
    onClick = {
        painter.restart()
    }
) {
    Text("Reload Image")
}

Image(
    painter = painter,
    contentDescription = null,
    modifier = Modifier.then(sizeResolver),
)

Make sure you have at least this dependency version in your build.gradle:

implementation("io.coil-kt.coil3:coil-compose:3.2.0")
like image 55
BenjyTec Avatar answered Dec 03 '25 10:12

BenjyTec



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!