Android webview loadDataWithBaseURL 如何从资产加载图像? [英] Android webview loadDataWithBaseURL how load images from assets?
本文介绍了Android webview loadDataWithBaseURL 如何从资产加载图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的项目中,我有一个文件:
In my project I have a files:
"MyProject/assets/folder1/image1.jpg"
"MyProject/assets/folder1/index.html".
在 webView 中,我需要打开 index.html(带图片).
In webView I need to open index.html (with images).
我正在尝试此代码:
String baseUrl = "file:///android_asset/folder1/";
webView.loadDataWithBaseURL(baseUrl, readFileAsString("index.html") , mimeType, "UTF-8", null);
但图片无法加载.
如果我将图像放到assets"目录(MyProject/assets/
)并制作 baseUrl = "file:///android_asset"
图像加载正确;
If I put images to "assets" directory (MyProject/assets/
) and make baseUrl = "file:///android_asset"
images are loaded correctly;
如何不仅从根资产目录加载图像,而且还从 assets/folder1
加载图像?
How load images not only from root assets directory, but and from assets/folder1
?
推荐答案
试试这个
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);
有关更多信息,请尝试此链接
For more information try this link
这篇关于Android webview loadDataWithBaseURL 如何从资产加载图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文