【发布时间】:2018-10-03 08:34:52
【问题描述】:
我正在尝试使用 html 在 web 视图中显示本地图像文件中的图像。
但由于某种原因,图像没有加载,我收到以下消息:
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 的权限。
<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 文本如下所示:
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:
mBinding.webView.loadData(html, "text/html", "UTF-8");
【问题讨论】:
-
1.
file方案必须以三个斜杠开头,例如:file:///sdcard/01.txt2. 您可以访问仅放在您的包文件夹中的文件。{package_name_here}只能是你的包名 -
是的,完全正确。我自己发现的,但感谢您的回答。
标签: file webview resources local