Skip to content

Instantly share code, notes, and snippets.

@KatieBarnett
Created January 13, 2025 05:56
Show Gist options
  • Save KatieBarnett/71eed8b08d97e955262ae3b70dbb0e95 to your computer and use it in GitHub Desktop.
Save KatieBarnett/71eed8b08d97e955262ae3b70dbb0e95 to your computer and use it in GitHub Desktop.
Downloading an image to a file in a Coroutine Worker - Part 1
@OptIn(ExperimentalCoilApi::class)
private suspend fun downloadImage(
url: String,
context: Context,
applicationContext: Context,
force: Boolean
): String {
val request = ImageRequest.Builder(context)
.data(url)
.build()
// Request the image to be loaded and throw error if it failed
with(context.imageLoader) {
if (force) {
diskCache?.remove(url)
memoryCache?.remove(MemoryCache.Key(url))
}
val result = execute(request)
if (result is ErrorResult) {
throw result.throwable
}
}
// Next... find the image in the cache
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment