0

This probably is covered somewhere, but I was unable to find it in the docs / on SO, so I decided to ask:

I've tried this:

@Composable
fun RawImage(
    modifier: Modifier = Modifier,
    @RawRes resource: Int,
    contentDescription: String? = null,
    contentScale: ContentScale = ContentScale.Fit
) {
    AsyncImage(
        modifier = modifier,
        model = LocalContext.current.resources.openRawResource(resource),
        contentDescription = contentDescription,
        contentScale = contentScale
    )
}

and this:

@Composable
fun RawImage(
    modifier: Modifier = Modifier,
    @RawRes resource: Int,
    contentDescription: String? = null,
    contentScale: ContentScale = ContentScale.Fit
) {
    AsyncImage(
        modifier = modifier,
        model = ImageRequest.Builder(LocalContext.current)
            .data(resource)
            .build(),
        contentDescription = contentDescription,
        contentScale = contentScale
    )
}

but so far, both of them don't seem to load the image, so I'm seeking any resources / gists / links that describe how to display a RawRes in Compose, cheers!

Tenfour04
  • 83,111
  • 11
  • 94
  • 154
JustSightseeing
  • 1,460
  • 3
  • 17
  • 37
  • Is there a requirement for it to be a raw resource rather than a `nodpi` drawable resource or an asset? – Tenfour04 Jul 24 '23 at 18:17
  • It is a .jpg image, so I believe so? – JustSightseeing Jul 24 '23 at 19:24
  • That’s no reason it couldn’t be either of those other options. A `nodpi` resource would be easiest so you wouldn’t need to use Coil, but Coil can load assets directly using the `file://` prefix for the URI. – Tenfour04 Jul 24 '23 at 20:10
  • managed to solve it with `nodpi`, would you mind posting the comment as an answer, so I may mark it as the solution? – JustSightseeing Jul 24 '23 at 20:33

1 Answers1

1

If you don't necessarily need to use a raw resource, you can use a drawable-nodpi resource and load it as you would other image resources, which is simpler than trying to use a raw resource. .jpg files are valid drawable resources.

Tenfour04
  • 83,111
  • 11
  • 94
  • 154