I am in transition to replacing Glide with Coil.
In Glide, there is an easy option to Cache web view images to load images faster. Is the same available for Coil?
The main issue I am facing in Coil is how to return the WebResourceResponse
The following is how I use Glide while compressing the bitmap.
override fun shouldInterceptRequest(
view: WebView?,
request: WebResourceRequest?
): WebResourceResponse? {
try {
val url = request!!.url.toString()
if (url.lowercase().contains(".jpg") || url.lowercase().contains(".jpeg")) {
val bitmap =
Glide.with(webView).asBitmap().diskCacheStrategy(DiskCacheStrategy.ALL)
.load(url).submit().get()
return WebResourceResponse(
"image/jpg",
"UTF-8",
getBitmapInputStream(bitmap, Bitmap.CompressFormat.JPEG)
)
} else if (url.lowercase().contains(".png")) {
val bitmap =
Glide.with(webView).asBitmap().diskCacheStrategy(DiskCacheStrategy.ALL)
.load(url).submit().get()
return WebResourceResponse(
"image/jpg",
"UTF-8",
getBitmapInputStream(bitmap, Bitmap.CompressFormat.PNG)
)
} else {
return super.shouldInterceptRequest(view, request)
}
} catch (e: Exception) {
return super.shouldInterceptRequest(view, request)
}
}
}
}
private fun getBitmapInputStream(bitmap: Bitmap, compressFormat: CompressFormat): InputStream {
val byteArrayOutputStream = ByteArrayOutputStream()
bitmap.compress(compressFormat, 80, byteArrayOutputStream)
val bitmapData = byteArrayOutputStream.toByteArray()
return ByteArrayInputStream(bitmapData)
}
I tried replacing the Gldie code with Colin using the following, but it’s a suspend function and unable to return the data.
val request = ImageRequest.Builder(requireContext())
.data(url)
.build()
val drawable = requireActivity().imageLoader.execute(request).drawable?.toBitmap()
I would appreciate any help.
Thanks in advance