Ninad
Ninad

Reputation: 1871

Images not loading in android webview

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 ?

Upvotes: 31

Views: 42637

Answers (9)

Gines Frank Ellis
Gines Frank Ellis

Reputation: 11

I add all the comments here til the image shows up, hahaha

android:usesClearTextTraffic="true"

work for the final try, maybe it depends on what type of image the website is using, just add all the syntaxes of all their answers here til the image shows up

Upvotes: 0

Swarnava Chakraborty
Swarnava Chakraborty

Reputation: 147

My problem was just one image, not loading in webview. I just add this two thing

In Manifest

android:usesCleartextTraffic="true"

In java oncreate() just add

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
         mywebView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
}

Upvotes: 1

Md.Tarikul Islam
Md.Tarikul Islam

Reputation: 1301

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);

You may need this one as well, if you are lazy-loading content:

      webView.getSettings().setJavaScriptEnabled(true);

Upvotes: 17

Varun P V
Varun P V

Reputation: 1233

I have faced the same issue. In the web view, the Image with the http URL was not loading in my app. The following solution fixed my issue,

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

But in Android 9, the above solution has thrown the issue sometimes, in such case add the following line in the manifest file.

android:usesCleartextTraffic="true"

Upvotes: 2

Junaed
Junaed

Reputation: 1957

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://')

Upvotes: 4

Idan Damri
Idan Damri

Reputation: 194

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);

Upvotes: 2

user6720362
user6720362

Reputation:

Just this will suffice

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

Upvotes: 8

Vrashabh Irde
Vrashabh Irde

Reputation: 14367

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.

Upvotes: 22

ifiok
ifiok

Reputation: 494

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();

            }
        });

Upvotes: 3

Related Questions