2

I have a webview showing a local html page resource in a string like this:

<p>Hi.</p>

<p><img alt="" src="http://ADRESS.COM/image.jpg" style="height:300px; width:400px" /></p>

and doing that with this code:

    WebView web = (WebView) findViewById(R.id.web);
    web.getSettings().setJavaScriptEnabled(true);
    web.loadDataWithBaseURL("",notice,"text/html","UTF-8","");

The page contains some images with online src. but in android WebView the images doesn't load. (in chrome it works). What I should do?

thanks.

Shashanth
  • 4,995
  • 7
  • 41
  • 51
Saeed
  • 97
  • 9

1 Answers1

4

Add these two lines in your code.

web.getSettings().setLoadsImagesAutomatically(true);
web.getSettings().setDomStorageEnabled(true);

Instead of using web.loadDataWithBaseURL("",notice,"text/html","UTF-8",""); simply load your website url as follows

web.loadUrl("http://www.google.com");

if you want load your local html page. Create asset directory android then put your html file in asset folder and and set it as follows

web.loadUrl("file:///android_asset/yourFile.html");
Saurabh Bhandari
  • 2,438
  • 4
  • 26
  • 33
  • still doesn't load images. – Saeed Jan 14 '17 at 15:37
  • because you are loading webpage html with text format – Saurabh Bhandari Jan 14 '17 at 16:06
  • I edited the answer @SaeedKetabi please check it . If it works for you then don not forget to up vote. Thank you – Saurabh Bhandari Jan 14 '17 at 16:15
  • But i don't want load any site. i load just a html local page source. @SaurabhBhandari – Saeed Jan 14 '17 at 16:16
  • @SaeedKetabi now check my answer. if you dont how to create asset folder then follow this link http://code2care.org/2015/create-assets-folder-in-android-studio/ – Saurabh Bhandari Jan 14 '17 at 16:28
  • But i should to create a html file in asset folder when app is runnig. becuase i get the html file resource online in a string. – Saeed Jan 14 '17 at 17:20
  • I get a html file source containing text and image from server and now i want to load it into my Activity. Images are uploaded on server and now Webview doesn't load images. – Saeed Jan 14 '17 at 17:30
  • @SaeedKetabi webView loads web pages not a particular part of a web page. if your webpage working in your laptop or PC browser then it will run in your app. – Saurabh Bhandari Jan 14 '17 at 17:41