1

I need to reload/refresh the coil image because it has been rewritten to disk/storage The code I am using

val context = LocalContext.current
    val loader = if (!isSvg) {
        LocalImageLoader.current ?: remember { ImageLoader(context) }
    } else {
        remember {
            ImageLoader.Builder(context)
                .componentRegistry {
                    add(SvgDecoder(context))
                }.build()
        }
    }

   val painter = rememberCoilPainter(request = request, imageLoader = loader)


    LaunchedEffect(key1 = invalidate, block = {
        val result = loader.execute(
            ImageRequest.Builder(context)
                .diskCachePolicy(if (fromCache) CachePolicy.ENABLED else CachePolicy.DISABLED)
                .data(request).build()
        )
        Log.d("TL_ImageLoadRes", result.toString())
    })

    Box(modifier = Modifier.background(MaterialTheme.colors.onBackground.copy(.4f))) {
        Image(
            modifier = modifier,
            painter = painter,
            contentDescription = stringResource(R.string.random_image),
            contentScale = contentScale
        )
    }

Every time invalidate changes , it means the image has been rewritten and now may be found and I execute the LaunchedEffect but after getting the success result I don't know where to put it , it contains a Drawable , I don't know what to do with it

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Waqas Tahir
  • 7,171
  • 5
  • 25
  • 47
  • have you managed to solve this issue? – wilddev Sep 08 '21 at 17:37
  • 1
    yes , I use a mutable state and set it false to undisplay the image and then use a delay in a coroutine to display it again this triggers the coil image to reload it , I use a 1 sec delay , this is not ideal because the images won't display for a sec but it works ! let me know if you need code help , I'll add an answer – Waqas Tahir Sep 09 '21 at 08:27

1 Answers1

1

Inside LaunchedEffect block, add below lines to refresh the painter

painter.onForgotten()
painter.onRemembered()
Krish4906
  • 121
  • 1
  • 1
  • 9