1

I am wondering how do I set the path for WebViews loadWithBaseURL correctly.

What I want to do is, to load html in a webview, that uses resources that are stored on the external storage.

For Example:

<html>
<head>
<style>body{ background-image:url(beach.jpg); }</style> 
</head>
<body> 
<img src="football.jpg" />
</body>
</html>

Where beach.jpg and ball.jpg are stored directly in the "root" directory of the phones external storage (/sdcard/beach.jpg and /sdcard/ball.jpg)

So I tried to load the content as follows:

String html = "<html> ... example from above ... </html>";
String base = Environment.getExternalStorageDirectory().getAbsolutePath().toString();
webView.loadDataWithBaseURL("file://" + base, html, "text/html", "utf-8", null);

However the path seems to be wrong, because I can't see the image in the webview. Any suggestions?

sockeqwe
  • 15,574
  • 24
  • 88
  • 144

2 Answers2

2

Have you enabled file access on the webview?

webView.getSettings().setAllowFileAccess(true);

Additionally, if you are constructing the HTML yourself - you might consider using full paths for the images.

String html = "<html>... <img src=\"file://"+base+"/football.jpg\" />";
NPike
  • 13,136
  • 12
  • 63
  • 80
0

Have you give internet permission?

<uses-permission android:name="android.permission.INTERNET" /> 

These may also help..

Community
  • 1
  • 1
ridoy
  • 6,274
  • 2
  • 29
  • 60