Andras Sanislo
Andras Sanislo

Reputation: 1209

Android WebView Not allowed to load local resource

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"

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);

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>";

I am loading the html like this:

mBinding.webView.loadData(html, "text/html", "UTF-8");

Upvotes: 2

Views: 6799

Answers (1)

Andras Sanislo
Andras Sanislo

Reputation: 1209

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

Upvotes: 1

Related Questions