Referencing drawables in HTML in Android’s WebView
Here’s another interesting Android quirk:
So let’s say you have a webview that you want to populate with HTML from an arbitrary source, and you want to use an image in the res/drawable directory. You could try webview.loadData(html, “text/html”, “UTF-8″), and within the HTML, use
<img src="file:///android_res/drawable/icon.png" />
or something, but that won’t work. Instead, this is what I had to do:
WebView webview = (WebView)this.findViewById(R.id.webview); String html = "<html><head><title>TITLE!!!</title></head>"; html += "<body><h1>Image?</h1><img src="icon.png" /></body></html>"; webview.loadDataWithBaseURL("file:///android_res/drawable/", html, "text/html", "UTF-8", null);
So basically, for some reason, you absolutely have to use loadDataWithBaseURL.
–
UPDATE
This only seems to work with 2.2 (API level and up.