Skip to content

Instantly share code, notes, and snippets.

@KatieBarnett
Last active January 13, 2025 04:48
Show Gist options
  • Select an option

  • Save KatieBarnett/a773aafbbf9d4480752ac87c8e2a6313 to your computer and use it in GitHub Desktop.

Select an option

Save KatieBarnett/a773aafbbf9d4480752ac87c8e2a6313 to your computer and use it in GitHub Desktop.
Loading image bitmap using Coil
val context = LocalContext.current
var loadedBitmap by remember(imageUrl) { mutableStateOf<Bitmap?>(null) }
LaunchedEffect(imageUrl) {
withContext(Dispatchers.IO) {
val request = ImageRequest.Builder(context).data(imageUrl).apply {
memoryCachePolicy(CachePolicy.DISABLED)
diskCachePolicy(CachePolicy.DISABLED)
}.build()
// Request the image to be loaded and return null if an error has occurred
loadedBitmap = when (val result = context.imageLoader.execute(request)) {
is ErrorResult -> null
is SuccessResult -> result.drawable.toBitmapOrNull()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment