Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android add image to webview from a drawable

Tags:

html

android

I'd like something:

s="
<body>

<img src='"+R.drawable.picture+"'>

</body>";

How can I do this?

like image 911
lacas Avatar asked Dec 26 '10 13:12

lacas


2 Answers

After some experimenting I found that the following HTML worked on Android 2.3.1 (but not on older versions):

<img src="file:///android_res/drawable/example.png"/>

The really cool part is that the image file was actually in the directory drawable-hdpi but the resource manager provides the image in the standard directory drawable.

like image 62
Eric Obermühlner Avatar answered Sep 24 '22 08:09

Eric Obermühlner


You can only do such a thing if our image is inside your /assets folder. Also, you must load your html with a baseUrl that's inside your assets folder.

You can use WebView.loadUrl() or WebView.loadDataWithBaseURL():

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

or

webView.loadDataWithBaseURL("file:///android_asset/", "<img src='file.jpg' />", "text/html", "utf-8", null);

(file.jpg should be inside your assets folder)

like image 25
Jonas Alves Avatar answered Sep 25 '22 08:09

Jonas Alves