5

I use Coil to load .svg resources from the web, for example:

@Composable
fun Widget(modifier: Modifier = Modifier) {
    AsyncImage(
        modifier = Modifier.fillMaxSize(),
        model = ImageRequest.Builder(LocalContext.current)
            .decoderFactory(SvgDecoder.Factory())
            .data("https://www.svgrepo.com/show/326168/circle-large.svg")
            .build(),
        contentDescription = null,
        contentScale = ContentScale.FillWidth
    )
}

It loads successfully when running on devices, but when running in Compose Preview, no resources is displayed. How to have Compose Preview load and display resources fetched from the Web?

hescul
  • 71
  • 5
  • 2
    I don't think it's possible to access the network inside the preview. You need to run on the real device or use the placeholder parameter to display the local resource which will render inside the preview. [Preview Limitations](https://developer.android.com/jetpack/compose/tooling#:~:text=in%20some%20limitations%3A-,No%20network%20access,-No%20file%20access) – Priyank Jain Jul 06 '22 at 14:19
  • Local resource is not shown as well, async loader is not loading async during the preview. – DmitryBorodin Jul 13 '23 at 20:54

1 Answers1

0

I think the only work around is to manually set the image if it is a preview, like so:

if (isPreview) {
     Image(
         painterResource(id = R.drawable.my_test_image),
         ...
     )
 } else {
     AsyncImage(
         ...
     )
 }
Ben Gooding
  • 884
  • 9
  • 18