How to load Image Files into Jetpack Compose Image using Coil

Viewed 4879

I would like to load saved image on cache into Image of Jetpack Compose using Coil. I searched in stackoverflow but all suggested ways are for loading web urls. I know that It's possible to convert image file to bitmap and then pass it to Coil but is there any builtin solution for it?

2 Answers

You can use Coil to load any object as a data:

val cacheFile = File(context.cacheDir, "filename")
Image(
    rememberImagePainter(cacheFile),
    contentDescription = "...",
)

If you've a locally saved image path, you can try the following method,

val painter = rememberImagePainter(data = File(filePath))
Image(
    painter = painter,
    contentDescription = null)

and to load image from remote url,

val painter = rememberImagePainter(data = imageUrl)
Image(
    painter = painter,
    contentDescription = null)
Related