Skip to content Skip to sidebar Skip to footer

@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

Solution 1:

This worked in android 2.2

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

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

Solution 2:

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.

WebViewwebView=newWebView(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:

<imgsrc="res/drawable/icon.png"/>

Does that work for you?

Solution 3:

user302750,

Your method is incorrect. res/drawable/banner300.jpg should be file:///android_res/drawable/banner300.jpg.

banner300.jpg can be in one of the following locations in your project, and android will automatically load the correct one for the device you're using.

  • res/drawable/banner300.jpg
  • res/drawable-ldpi/banner300.jpg
  • res/drawable-mdpi/banner300.jpg
  • res/drawable-hdpi/banner300.jpg
  • res/drawable-xhdpi/banner300.jpg

The assets folder is for generic resources that you don't need different versions of. An example might be an XSL template, or something of that nature.

MikeNereson, your response to your problem is also incorrect, drawable resources should be contained in the res directory.

Here's an example from my own project. I output html, exactly like this:

<imgsrc="file:///android_res/drawable/stats_btn.png"/>

If using an ldpi device, I get the ldpi image, if using hdpi or mdpi, I get the appropriate one for those as well. Android resolves file:///android_res/drawable/stats_btn.png to one of the following resources that I have in my project.

  • ./res/drawable-ldpi/stats_btn.png
  • ./res/drawable-hdpi/stats_btn.png
  • ./res/drawable-mdpi/stats_btn.png

Solution 4:

From my tests the path, "file:///android_res/drawable/q1.png" only works with 2.2+. For 2.1 that gives a file not found error. Moving the resource to assets will work but that isn't really feasible if you want to make use of the drawable features (meaning you'd need a copy of the image in each folder...).

Solution 5:

Answers involving file:// URLs may not work any more as of Android 4.0. See answer to this question. A way that will work is in the answer here.

Post a Comment for "@android Display /res/viewable In Webview"