Replies: 3 comments 2 replies
-
Agreed, using an OkHttp interceptor (either regular or network) should solve this. Even though Coil now has its own DiskCache, it uses the same cache control logic as OkHttp. Optionally, you can set
I think your existing solution of using
Using
You'll have to use 2.x to write custom entries to the disk cache. For 1.x, it might be possible to use an OkHttp interceptor to decode the response stream and replace it with a stream to a resized image. For 2.x I think this should work: val dispatcher = Dispatchers.IO.limitedParallelism(5)
withContext(dispatcher) {
artworks.forEach { artwork ->
val request = ImageRequest.Builder(application)
.data(artwork)
.memoryCachePolicy(CachePolicy.DISABLED)
.dispatcher(dispatcher)
.build()
val result = imageLoader.execute(request)
imageLoader.diskCache?.edit(artwork)?.use { editor ->
val bitmap = result.drawable.toBitmap()
FileOutputStream(editor.data).use { sink ->
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, sink)
}
bitmap.recycle()
}
}
} Hope that helps! 🙌 |
Beta Was this translation helpful? Give feedback.
-
nit: this doesn't seem like an compiling example
Requires this extension method https://github.com/coil-kt/coil/blob/8b378c0e0c4bc2b676d19c974959552d089a100d/coil-base/src/test/java/coil/disk/DiskCacheTest.kt |
Beta Was this translation helpful? Give feedback.
-
From the example above, I actually want to reduce the image size (width/size), as well as file size. Is that actually possible via ImageRequest? or this approach, but checking the size of the bitmap is warranted. Also if there is a possibility of SVG, how to I detect this in the listener, and avoid resizing and saving a new Bitmap? |
Beta Was this translation helpful? Give feedback.
-
I'm trying to prefetch images for a Media app for artwork previews. The usage of them is at a small size, but I don't have control of the source images which may end up being quite large.
Issues I'm concerned about
For the first point, I have an OkHttp interceptor that sets a very long tolerant cache policy for responses of image/ type.
For the rest I'm trying the following
Does the dispatcher set on ImageRequest limit requests in flight? Or just slow it down by limiting the number of in flight threads?
How do I cache resized images? Use Coil 2 so the cache is above the HTTP layer?
Beta Was this translation helpful? Give feedback.
All reactions