-
-
Save KatieBarnett/71eed8b08d97e955262ae3b70dbb0e95 to your computer and use it in GitHub Desktop.
Downloading an image to a file in a Coroutine Worker - Part 1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @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