开发者

Android add image to webview from a drawable

I'd like something:

s="
<body>

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

</body>";

How can I开发者_StackOverflow do this?


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)


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.


Try this:

 web_object.loadDataWithBaseURL("file:///android_res/drawable/", "<img src='test.jpg' />", "text/html", "utf-8", null);

no need to make seprate html file


Set Image Width 100%.

wv.loadDataWithBaseURL("file:///android_res/drawable/", "<img src='"+ url + "' style='width:100%' />", "text/html", "utf-8", null); 
    wv.getSettings().setBuiltInZoomControls(true);
    wv.getSettings().setDisplayZoomControls(false);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜