Images not loading in android webview

北城余情 提交于 2019-12-21 03:21:37

问题


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 ?


回答1:


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.




回答2:


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



回答3:


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

            }
        });



回答4:


Just this will suffice

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



回答5:


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


来源:https://stackoverflow.com/questions/29888395/images-not-loading-in-android-webview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!