android - WebViewClient shouldInterceptRequest Freezes the WebView -


i want cache images displayed in webview specific time, e.g. 7 days, needed both save image caches disk , load them disk , provide them webview.

also needed resize images avoid webview; crash on high memory usage, implemented caching , resizing logic in blocking function named "fetchbitmap".

as android documentation states, "shouldinterceptrequest" runs on thread other ui thread, can networking in function, can see, blocking call causes webview freeze.

the way function behaves forces me use blocking call, , can't pass runnable e.g. 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, changed logic, block read disk, , handle network load (caching) separately in runnable; "fetchbitmapforwebview"; doubles network load when cache not available, ui 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; } 

the general principle shouldn't heavy processing inside shouldinterceptrequest, synchronous call (albeit on thread). calls network requests processed on same thread blocking 1 request cause of other requests blocked well.

for use case, consider running small web server inside app, required caching , processing. surely, need rewrite links inside displayed web pages in order point local server instead of original web server, may cumbersome in situations. general approach off-loading heavy work , maintaining local cache.

there numerous simple java web servers around, example:


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -