Android WebView不允许加载本地资源 [英] Android WebView Not allowed to load local resource
问题描述
我正在尝试使用html在Webview中显示本地图像文件中的图像.
I am trying to display an image from a local image file in a webview using html.
但是由于某些原因,图像未加载,因此我收到以下消息:
But fore some reason the image is not loading, and I get the following message:
I/chromium: [INFO:CONSOLE(0)] "Not allowed to load local resource: file://data/user/0/{package_name_here}/files/images/382d26c2-5ff1-4082-93ab-2e34baa4ecbb.png"
我100%确定我已授予运行时权限和Webview的权限.
I am 100% sure that I have granted the runtime permissions and the permsissions for webview.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
wv.getSettings().setJavaScriptEnabled(true);
// Enable plugins
wv.getSettings().setPluginState(WebSettings.PluginState.ON);
// Increase the priority of the rendering thread to high
wv.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
// Enable application caching
wv.getSettings().setAppCacheEnabled(true);
// Enable HTML5 local storage and make it persistent
wv.getSettings().setDomStorageEnabled(true);
wv.getSettings().setDatabaseEnabled(true);
wv.getSettings().setAllowFileAccess(true);
wv.getSettings().setAllowFileAccessFromFileURLs(true);
wv.getSettings().setAllowContentAccess(true);
wv.getSettings().setAllowUniversalAccessFromFileURLs(true);
html文本看起来像这样:
The html text looks like this:
private String html = "<!DOCTYPE html>\n" +
"<html>\n" +
" <head>\n" +
" \n" +
" </head>\n" +
" <body>\n" +
" \t<div >\n" +
"\t\t \n" +
"\t\t <div><img src=\"file://data/user/0/package_name_here/files/images/382d26c2-5ff1-4082-93ab-2e34baa4ecbb.png\"/></div>\n" +
"\t\t</div>\n" +
" </body>\n" +
"</html>";
我正在像这样加载html:
I am loading the html like this:
mBinding.webView.loadData(html, "text/html", "UTF-8");
推荐答案
如don11995在下面的注释中所述,路径必须以3个斜杠开头:文件:///storage/emulated/0/Pictures/JPEG_20181001_155835_8344102725172812900.jpg
As mentioned by don11995 in a comment below, the path has to start with 3 slashes: file:///storage/emulated/0/Pictures/JPEG_20181001_155835_8344102725172812900.jpg
这篇关于Android WebView不允许加载本地资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!