将可绘制资源作为webview中的图像访问

时间:2011-06-09 18:23:45

标签: android resources android-webview

我的包中有一个png文件,我只想将它用作我在webview中通过html加载的图像的源。

到目前为止,我已尝试过,thisthisthis。其中没有一个真正起作用。

以下是我将img标记添加到html的方法。谁能帮我?我正在尝试尽可能早地构建这个版本(目前为4 [1.6])

String stringPath = "fie:///android_asset/chat_bubble.png";
addOn = String.format("<img src=\"%s\" >", stringPath);

3 个答案:

答案 0 :(得分:4)

(我假设您复制了项目中的代码片段。)

有一个拼写错误:

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

应该是

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

您没有关闭<img>代码:

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

应该是

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

答案 1 :(得分:2)

这对我有用,你认为合适的概括:

1&GT;将PNG文件添加到res / assets目录 2 - ;创建一个包含要显示的所有HTML代码的字符串 3 GT;请确保对要显示的图像使用以下格式:

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

以下是示例代码:

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



答案 2 :(得分:1)

尝试this thread,但您应该使用drawables。

此致  斯特凡

相关问题