Using images from HTML in assets directory

The last post described how to show an HTML file in Android from the assets directory in the WebView.

Sooner or later you will want to show images in the HTML.

Solution 1 : Images in assets directory

The easy and obvious way is to put the images in the assets directory.

<html>
<p>This is an image:
<img src="image1.png"/>
</p>
</html>

This works fine but it has the disadvantage that you maybe already have the images in the res/drawable directory (for example the menu icons when you are writing the help documentation).

Let’s see whether there is another solution.

Solution 2 : Images in drawable directory

This solution only works in Android 2.3 and later!

<html>
<p>This is an image:
<img src="file:///android_res/drawable/image2.png"/>
</p>
</html>

This is analogous to the URL used to access the assets directory.

Directory URL
/assets file:///android_assets/
/res/drawable file:///android_drawable/

On older platforms I haven’t found a better way so you are stuck with the assets directory.

This entry was posted in Android, Development, Tips and Tricks and tagged , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *