Images not loading in android webview-ThrowExceptions

Exception or error:

I am trying to load the site in an android app web view.

The site loads without the images ,all the images from the site are not loaded what could be the problem.

The code for onCreate is shown below.

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);
    String url = getResources().getString(R.string.web_url);
    web = (WebView) findViewById(R.id.webview01);
    progressBar = (ProgressBar) findViewById(R.id.progressBar1);

    web.setWebViewClient(new myWebClient());
    web.getSettings().setJavaScriptEnabled(true);
    web.getSettings().setUseWideViewPort(true);
    web.loadUrl(url);
}

One more note, when i set javascript to false using web.getSettings().setJavaScriptEnabled(false); the images load giving a warning that javascript should be enabled to run the site properly.

This site is protected with CloudFlare ,could this be the reason images are not loaded in the android web view ?

How to solve:

Add

web.getSettings().setDomStorageEnabled(true);

This should work. Keep the rest as is.

Although a caveat, I don’t think its recommended to enable this by default, its disabled by default. The setting allows a website to store data and reuse it. So the images are not being loaded, because this site wasn’t allowed to store them on your device. This opens up a can of security concerns.

###

Just use this line of code. I think your problem will be solved.

     webView.getSettings().setDomStorageEnabled(true);
     webView.getSettings().setAppCacheEnabled(true);
     webView.getSettings().setLoadsImagesAutomatically(true);
     webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);

###

Just this will suffice

webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);

###

I suspect that the issue might be your webview client.
Use this instead:

webView.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Log.i(TAG, "Processing webview url click...");
                view.loadUrl(url);
                return true;
            }

            public void onPageFinished(WebView view, String url) {
                Log.i(TAG, "Finished loading URL: " +url);
                if (pDialog.isShowing()) {
                    pDialog.dismiss();
                }
            }

            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                Log.e(TAG, "Error: " + description);
                Toast.makeText(FundCardWeb.this, "Page Load Error" + description, Toast.LENGTH_SHORT).show();

            }
        });

###

I had similar problem in Android 9.0. The images in the site’s html were using http instead of https.
Then I changed all the http with https and everything worked!
It was very easy to change the http to https using a sql query in mySql.

I am giving the query if it helps anyone!

UPDATE table_name SET column_name = replace(column_name, '<img src="http://', '<img src="https://')

###

for me the combination of adding those did it:

webSettings.setDomStorageEnabled(true);
webSettings.setAppCacheEnabled(true);
webSettings.setLoadsImagesAutomatically(true);

I don’t like adding unneccesery cod when so I didn’t use:

webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);

Leave a Reply

Your email address will not be published. Required fields are marked *