Android WebView不允许加载本地资源

时间:2018-10-03 08:34:53

标签: file webview resources local

我正在尝试使用html在Webview中显示本地图像文件中的图像。

但是由于某些原因,图像未加载,因此我收到以下消息:

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 个答案:

答案 0 :(得分:0)

如don11995在下面的评论中所述,路径必须以3个斜杠开头: 文件:///storage/emulated/0/Pictures/JPEG_20181001_155835_8344102725172812900.jpg