I want to cache images displayed in the WebView for a specific time, for e.g. 7 days, so I needed to both save image caches to disk and load them from disk and provide them to the WebView.
Also I needed to resize the images to avoid the WebView; Crash on High Memory Usage, so I implemented the caching and resizing logic in a Blocking Function named "fetchBitmap".
As the Android documentation states, the "shouldInterceptRequest" runs on a Thread Other than the UI Thread, so I can do networking in this function, but as I can see, a blocking call causes the WebView to Freeze.
The way the function behaves forces me to use a blocking call, and I can't pass a Runnable for e.g. for a future completion.
Any workarounds?
webView.setWebViewClient(new WebViewClient() {
private boolean isImage(String url) {
if (url.contains(".jpg") || url.contains(".jpeg")
|| url.contains(".png")) {
return true;
}
return false;
}
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
if (isImage(url)) {
Bitmap bitmap = ImageUtil.fetchBitmap(url);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 100, bos);
byte[] bitmapdata = bos.toByteArray();
ByteArrayInputStream bs = new ByteArrayInputStream(bitmapdata);
return new WebResourceResponse("image/*", "base64", bs);
}
return null;
}
});
--UPDATE--
OK, I changed the logic, now I just Block Read from the disk, and handle the network load (caching) separately in a runnable; "fetchBitmapForWebView"; it doubles the network load when the cache is not available, but the UI is now more responsive.
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
if (isImage(url)) {
Bitmap bitmap = ImageUtil.fetchBitmapForWebView(url);
if (bitmap == null) {
return null;
}
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 100, bos);
byte[] bitmapdata = bos.toByteArray();
ByteArrayInputStream bs = new ByteArrayInputStream(bitmapdata);
return new WebResourceResponse("image/*", "base64", bs);
}
return null;
}