开发者

Access a drawable resource as an image in a webview

开发者 https://www.devze.com 2023-03-11 23:53 出处:网络
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.

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,开发者_StackOverflow 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);


(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);


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);




Try this thread, but you should use drawables.

Regards, Stéphane

0

精彩评论

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