我正在尝试加载一个存储在数据库中的html字符串,其中包含一个图像到一个WebView
中。图像存储在内部存储器中。我给出了一个html string.But的参考,它不工作。有帮助吗?
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();
发布于 2021-01-04 22:04:29
在最新的安卓版本中,我们无法从存储中访问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");
https://stackoverflow.com/questions/42266264
复制相似问题