最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

android - How to prevent AsyncImage from using cached images? - Stack Overflow

programmeradmin3浏览0评论

I'm using Coil library in Jetpack Compose to load images from network with AsyncImage. However, I noticed that AsyncImage caches the image, so if the same URL is used again, it loads from the cache instead of fetching the updated image.

This causes an issue in my case because the image at the URL may have changed, but AsyncImage still displays the old cached version.

How can I prevent AsyncImage from using cached images and force it to always load the latest version?

I'm using Coil library in Jetpack Compose to load images from network with AsyncImage. However, I noticed that AsyncImage caches the image, so if the same URL is used again, it loads from the cache instead of fetching the updated image.

This causes an issue in my case because the image at the URL may have changed, but AsyncImage still displays the old cached version.

How can I prevent AsyncImage from using cached images and force it to always load the latest version?

Share Improve this question asked Feb 14 at 7:21 Nguyen Tien DungNguyen Tien Dung 6362 gold badges10 silver badges34 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 3

For your use-case, you can disable cache policy as below:

@Composable
fun FreshImage(url: String) {
    val context = LocalContext.current

    AsyncImage(
        model = ImageRequest.Builder(context)
            .data(url)
            .diskCachePolicy(CachePolicy.DISABLED)    
            .memoryCachePolicy(CachePolicy.DISABLED) 
            .build(),
        contentDescription = "Fresh image"
    )
}

You can solve your issue by updating the cache policy. For more details, please refer to this answer by @Phil Dukhov for an in-depth explanation of Coil's caching.

发布评论

评论列表(0)

  1. 暂无评论