question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Reload image after a failed load in Jetpack Compose

See original GitHub issue

Is your feature request related to a problem? Please describe.

val painter = rememberImagePainter(url)
Image(
    modifier = Modifier.fillMaxSize(),
    painter = painter,
    contentDescription = null,
    contentScale = ContentScale.Fit
)
when (val state = painter.state) {
    is ImagePainter.State.Error -> {
        TextButton(onClick = { }) { Text("retry") }
    }
}

After a failed load, the user should be able to reload the image via the retry button.

Describe the solution you’d like It is better to provide reload method, but it seems that setting request in ImagePainter to public would also work.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:3
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
colinrtwhitecommented, Mar 24, 2022

Still figuring out a good public API for this, but if you need this today you can force retry by changing a parameter:

var retryHash by remember { mutableStateOf(0) }
val painter = rememberAsyncImagePainter(
    model = ImageRequest.Builder(LocalContext.current)
        .data(url)
        .setParameter("retry_hash", retryHash, memoryCacheKey = null)
        .build()
)
Image(
    painter = painter,
    contentDescription = null,
    contentScale = ContentScale.Fit,
    modifier = Modifier.fillMaxSize(),
)
when (val state = painter.state) {
    is AsyncImagePainter.State.Error -> {
        TextButton(onClick = { retryHash++ }) { Text("retry") }
    }
}

Currently, I’m thinking the public API should be something like this, but let me know what you think!

val requestHandle = rememberAsyncImageRequestHandle()
val painter = rememberAsyncImagePainter(
    request = ImageRequest.Builder(LocalContext.current)
        .data(url)
        .requestHandle(requestHandle)
        .build()
)
Image(
    painter = painter,
    contentDescription = null,
    contentScale = ContentScale.Fit,
    modifier = Modifier.fillMaxSize(),
)
when (val state = painter.state) {
    is AsyncImagePainter.State.Error -> {
        TextButton(onClick = { requestHandle.restart() }) { Text("retry") }
    }
}
1reaction
FishHawkcommented, Nov 23, 2021

Thanks! I’ll try it later.

The api is indeed a problem. Your example is very similar to focusRequester. I’m ok with it. But since there must be an ImagePainter object here, I think it would be simpler to have an ImagePainter with a retry method.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jetpack compose - how do I refresh a screen when app ...
I came up with this: @Composable fun OnLifecycleEvent(onEvent: (owner: LifecycleOwner, event: Lifecycle.Event) -> Unit) { val eventHandler ...
Read more >
Loading images for Jetpack Compose using Glide, Coil, and ...
Landscapist is an image loading library for Jetpack Compose. There are three options; Glide, Coil, and Fresco. So you can choose by your...
Read more >
Asynchronously Load Images with Jetpack Compose - YouTube
The BEST android courses in the world: https://codingwithmitch.com/How to asynchronously load images using Jetpack Compose.
Read more >
Loading images | Jetpack Compose - Android Developers
Use the Image composable to display a graphic on screen. To load an image (for example: PNG, JPEG, WEBP) or vector resource from...
Read more >
Loading Image in Android Jetpack Compose Made Easy
It can be easily setup to handle Loading, Error and Success state. It can have fade in effect provided too, as shown below....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found