How to load Image Files into Jetpack Compose Image using Coil
Asked Answered
K

3

12

I want to load a local image file using Coil into a Jetpack Compose Image, but searching has produced only methods using web urls or by passing files converted to bitmaps.

Can Coil load a local image file directly into a Compose Image?

Kamakura answered 13/9, 2021 at 8:43 Comment(2)
Where are stored these files?Burrows
@Gabriele Mariotti on cache folderKamakura
M
15

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

Muscolo answered 27/12, 2022 at 18:6 Comment(0)
B
10

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

val cacheFile = File(context.cacheDir, "filename")
Image(
    rememberImagePainter(cacheFile),
    contentDescription = "...",
)
Burrows answered 13/9, 2021 at 9:5 Comment(2)
shouldn't you save the cacheFile via remember?Iberia
Currently it is deprecated in coil.Languet
K
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)
Kingcraft answered 15/9, 2021 at 4:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.