我试图将包含图像的数据库中存储的html字符串加载到WebView
中。图像存储在内部存储器中。我正在引用html字符串。但它不起作用。有什么帮助吗?
String content="<p>Can we have a rotational" +
" symmetry of order more than 1 whose angle of rotation is </p>\n" +
"\n <p>(i) <img alt=\"45\\degree\" src=\"file:///storage/emulated/01484890695248.jpg\" /></p>\n" +
"\n<p>(ii) <img alt=\"35\\degree\" src=\"file:///storage/emulated/01484890697301.jpg\" /></p>";
WebView00.loadDataWithBaseURL("", content, "text/html","UTF-8", "");
WebView00.getSettings();
答案 0 :(得分:10)
试试这个
String base = Environment.getExternalStorageDirectory().getAbsolutePath().toString();
String imagePath = "file://"+ base + "/image_name.jpg";
String html = "<html><head></head><body> <img src=\""+ imagePath + "\"> </body></html>";
webView.loadDataWithBaseURL("", html, "text/html","utf-8", "");
答案 1 :(得分:0)
将图像放入assets
文件夹。然后使用相应的前缀
file:///android_asset/
答案 2 :(得分:0)
问题出在文件路径
中file:///storage/emulated/01484890695248.jpg
将是
file:///storage/emulated/0/01484890695248.jpg
感谢@Charuක寻求帮助..
答案 3 :(得分:0)
在最新的 android 版本中,我们无法访问诸如 assets
之类的资源,存储中的文件。 https://developer.android.com/reference/androidx/webkit/WebViewAssetLoader 是继续操作的最佳指南。 WebViewAssetLoader
及其内部类有助于访问它们。
您可以查看以下示例代码。
final WebViewAssetLoader assetLoader = new WebViewAssetLoader.Builder()
.addPathHandler("/assets/", new WebViewAssetLoader.AssetsPathHandler(this)) //****for assets****
.addPathHandler("/images/", new WebViewAssetLoader.InternalStoragePathHandler(context, getFilesDir()))//****for files****
.build();
webView.setWebViewClient(new WebViewClient() {
@Override
public WebResourceResponse shouldInterceptRequest(WebView view,
WebResourceRequest request) {
return assetLoader.shouldInterceptRequest(request.getUrl());
}
});
String assetsPic = "<img width='42px' height='58px' src='https://appassets.androidplatform.net/assets/pic.png'/>";
String storagePic = "<img width='42px' height='58px' src='https://appassets.androidplatform.net/images/pic.jpg'/>";
webView.loadData(assetsPic+"<br>"+storagePic, "text/html", "UTF-8");