3

I have a png file in my package, and I just want to use it as the source for an image I am loading via html in a webview.

So far I have tried, this, this and this. None of which have actually worked.

Here is how I am adding the img tag to the html. Can anyone help me? I'm trying to build this for as early of an SDK version as possible (currently 4 [1.6])

String stringPath = "fie:///android_asset/chat_bubble.png";
addOn = String.format("<img src=\"%s\" >", stringPath);
Community
  • 1
  • 1
Dan F
  • 17,654
  • 5
  • 72
  • 110

3 Answers3

4

(I assume you copied the code piece from your project.)

There is a typo:

String stringPath = "fie:///android_asset/chat_bubble.png";

should be

String stringPath = "file:///android_asset/chat_bubble.png";

And you didn't closed <img> tag:

addOn = String.format("<img src=\"%s\" >", stringPath);

should be

addOn = String.format("<img src=\"%s\" />", stringPath);
faradaj
  • 3,629
  • 1
  • 22
  • 21
  • Turns out it was that damn missing 'l' the entire time >< hate typo bugs, thank you for spotting that – Dan F Jun 09 '11 at 18:52
  • @faradaj: Do you know how to do the same with the system resources, like `@android:drawable/ic_menu_info_details.png`? – Luis A. Florit Dec 22 '13 at 20:14
2

This worked for me, generalize as you see fit:

1> Add your PNG file to the res/assets dir.
2> Create a string that contains ALL of your HTML code to be displayed.
3> Make sure to use the below format for the image you want to display:

"<img src=\"file:///android_asset/my_image_goes_here.png\" />"

Here's the sample code:

final StringBuilder s = new StringBuilder();    

s.append("<html>");                             
s.append("<body>");
s.append("<img src=\"file:///android_asset/my_image_goes_here.png\" />");
s.append("</body>");                            
s.append("</html>");
myWebView.loadDataWithBaseURL(null, s.toString(), "text/html", "UTF-8", null);



1

Try this thread, but you should use drawables.

Regards, Stéphane

Community
  • 1
  • 1
Snicolas
  • 37,840
  • 15
  • 114
  • 173
  • Doesn't help at all, thats really just more of the same above. The accepted answer only works for 2.2+, and I use it in assets in my code currently – Dan F Jun 09 '11 at 18:48