Loading package html + resources into webview

user302750 picture user302750 · Jun 8, 2010 · Viewed 8.5k times · Source

I would like to know how I can display a HTML page in webview with references to relative/local images. The goal is to have the html page and all linked images contained in the android application package itself.

Where would I need to place the assets (assets directory?) and how do I reference these so they load into the webview?

Thanx! Sven

Answer

uaraven picture uaraven · Jan 28, 2011

You can put all html and images to assets directory, like:

assets\html\
  index.html
  image1.png
  image2.jpg

All references to the images in the html file should be in form of file:// url

<html>
<body>
  <img src="file:///android_asset/html/image1.png">
  <img src="file:///android_asset/html/image2.jpg">
</body>
</html>

After all, just load this HTML to WebView as usual:

   webView.loadUrl("file:///android_asset/html/index.html");