7

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?

Vahid
  • 1,588
  • 4
  • 22
  • 34

3 Answers3

8

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

val cacheFile = File(context.cacheDir, "filename")
Image(
    rememberImagePainter(cacheFile),
    contentDescription = "...",
)
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
7

For the new version of coil 2.2.2, rememberImagePainter didn't give me good results so I used this-

AsyncImage(
    model = ImageRequest.Builder(LocalContext.current)
        .data(<file_path>)
        .build(),
    contentDescription = "icon",
    contentScale = ContentScale.Inside,
    modifier = Modifier.size(30.dp)
)

Hope it helps someone

Priya Sindkar
  • 368
  • 3
  • 11
4

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)