Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@Android display /res/viewable in WebView

I am throwing HTML to a webview to render. In the HTML I need to load an image that I have in /res/drawable.

I have /res/drawable/my_image.png and code such as this:

final WebView browser = (WebView) findViewById(R.id.my_webview);
String html = new MyHelper(myObject).getHtml();
browser.loadDataWithBaseURL("", html, "text/html", "UTF-8", "");

Where the String html has something like:

<html><head>
<h1>Here is the image</h1>
<img src="my_image.png" />
</head><html>

The question is, what should that image src attribute be to refer to the image in /res/drawable?

like image 684
MikeNereson Avatar asked Apr 13 '10 15:04

MikeNereson


2 Answers

This worked in android 2.2

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

where q1.png is in the res/drawable-hdpi folder

like image 91
Superfuzz Avatar answered Oct 01 '22 12:10

Superfuzz


I admit I don't know much about WebViews / HTML, but it seems like you're taking the more complicated route for this. There's an easy way to load the HTML file; put it in your assets folder, and then it can be referred to as follows.

WebView webView = new WebView(this);
webView.loadUrl("file:///android_asset/index.html");
setContentView(webView);

Obviously your layout is more complex as you have more than just the WebView so you can't use setContentView() directly, but that's the basic idea. Then to reference an image in that HTML file, I used a <img> tag like so:

<img src="res/drawable/icon.png"/>

Does that work for you?

like image 40
Steve Haley Avatar answered Oct 01 '22 10:10

Steve Haley