1

I have a very complex bug: in my application, I use in webview that display local images. until version '76.0.3809.111' of the webview, everything was work perfectly but from this version when I'm trying to display those images I get "Failed to load resource: net::ERR_UNKNOWN_URL_SCHEME". This error occurs only in the first run when I close the application and restart it everything is working fine. Additionally when I set settings.setAppCacheEnabled(false) also everything is working perfectly. When I debug my application I noticed that in the first time (that the images not loaded) the "shouldInterceptRequest" methods not calling.

this is my settings of the webview :

    WebSettings settings = wv.getSettings();
    settings.setJavaScriptEnabled(true);
    settings.setJavaScriptCanOpenWindowsAutomatically(true);
    wv.addJavascriptInterface(jsHandler, "cpjs");
    settings.setDomStorageEnabled(true);
    String PACKAGE_NAME = ctx.getPackageName();
    settings.setDatabaseEnabled(true);
    settings.setDatabasePath("/data/data/" + PACKAGE_NAME + "/databases/");
    settings.setAppCacheMaxSize(1024 * 1024 * 16);
    settings.setAppCachePath(ctx.getCacheDir().getAbsolutePath());
    settings.setAllowFileAccess(true);
    settings.setAppCacheEnabled(true);
    settings.setLightTouchEnabled(false);
    settings.setSupportZoom(false);
    settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
    //Allow to redirect https to http for downloading content
    if (Build.VERSION.SDK_INT >= 21) {
        settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        KsLog.i(TAG, "WebView settings, MIXED_CONTENT_ALWAYS_ALLOW set");
    }
    settings.setSavePassword(false);
    settings.setLightTouchEnabled(false);
    settings.setSupportZoom(false);
    wv.setScrollContainer(false);
    wv.setHorizontalScrollBarEnabled(false);
    wv.setVerticalScrollBarEnabled(false);
    wv.setLongClickable(false);
    wv.cancelLongPress();

and this is the override of the "shouldInterceptRequest" method:

        @Override
        public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
            try {
                if (url.startsWith(Constants.KS_LOCAL_PREFIX)) {
                    // In case url starts with our proprietary protocol handle the request.
                    // Images are located in the external files directory under "images" folder
                    String fileName = url.substring(url.lastIndexOf(Constants.URL_SLASH));
                    String filePath = FileUtils.getImagesLocalFolder(getAppContext()) + fileName;

                    File imagefile = new File(filePath);
                    FileInputStream fis = new FileInputStream(imagefile);
                    Bitmap bi = BitmapFactory.decodeStream(fis);
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    if (bi != null) {
                        bi.compress(Bitmap.CompressFormat.PNG, 100, baos);
                    }
                    byte[] data = baos.toByteArray();
                    InputStream is = new ByteArrayInputStream(data);
                    return new WebResourceResponse("text/html", "UTF-8", is);
                }
            } catch (FileNotFoundException e) {
                KsLog.d(TAG, e.toString());
            } catch (NullPointerException nullEx) {
                KsLog.d(TAG, nullEx.toString());
            }
            return super.shouldInterceptRequest(view, url);
        }

This is an example of bad image : enter image description here

This is an example of good image : **enter image description here**

Thanks to everyone and I hope I explained myself well.

gil cohen
  • 333
  • 2
  • 8
  • 21
  • Did you manage to find a solution? I'm facing a very similar problem. – Tako Dec 23 '19 at 01:20
  • Yes, for me the solution was very simple and I just increase the delay for loading the URL on my webview. I think that it happens because the new engine of the chrome is more powerful and the page is loaded before the images were downloaded. – gil cohen Dec 23 '19 at 10:39
  • 1
    Thanks Gil, so it means shouldInterceptRequest() was getting called all along but just couldn't find the image, right? – Tako Dec 23 '19 at 21:46
  • exactly shouldInterceptRequest() was getting called – gil cohen Dec 24 '19 at 12:39
  • @gilcohen How exactly did you increase the delay for loading the URL in the webview? Did you use a handler? Can I request to see the code? Thank you. – Zim Jan 03 '20 at 08:28
  • very simple: handler with post delay of 5000 ms and then starting to load the webview – gil cohen Jan 05 '20 at 10:41

0 Answers0