I/chromium: [INFO:CONSOLE(0)] "Not allowed to load local resource: file:///android_asset/downloader.png",

Viewed 90

I create a button using java script function inside Vimeo script and I now wanted to change the button using my assets image but It give me this error.

I tried every possibility:

with(binding.webView) {
    settings.apply {
        pluginState = WebSettings.PluginState.ON
        displayZoomControls = true
        useWideViewPort = true
        loadWithOverviewMode = true

        javaScriptEnabled = true
        useWideViewPort = true
        setGeolocationEnabled(true)
        domStorageEnabled = true
        databaseEnabled = true
        setSupportMultipleWindows(true)
        setNeedInitialFocus(true)
        loadWithOverviewMode = true
        loadsImagesAutomatically = true
        setAppCacheEnabled(true)
        javaScriptCanOpenWindowsAutomatically = true
        blockNetworkImage = false
        builtInZoomControls = false
        mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
    }
    webViewClient = mWebViewClient
    webChromeClient = mWebChromeClient
    addJavascriptInterface(this, "Downloader")
    loadUrl("https://vimeo.com/watch")
}

I tried with Webview Assest

private val mWebChromeClient = object : WebChromeClient() {
    override fun onReceivedTitle(view: WebView?, title: String?) {
        super.onReceivedTitle(view, title)
    }

    override fun onConsoleMessage(message: ConsoleMessage): Boolean {
//            Log.e("tag**", "${message.message()} -- From line " +
//                    "${message.lineNumber()} of ${message.sourceId()}")
        return false
    }

}

private val assetLoader = WebViewAssetLoader.Builder()
    .addPathHandler("/assets/", WebViewAssetLoader.AssetsPathHandler(this))
    .build()

private val mWebViewClient = object : WebViewClient() {
    override fun shouldOverrideUrlLoading(
        view: WebView?,
        request: WebResourceRequest?
    ): Boolean {
        return super.shouldOverrideUrlLoading(view, request)
    }

    override fun onPageFinished(webView: WebView?, url: String?) {
        super.onPageFinished(webView, url)
        webView?.loadUrl(vimeoScript)
    }

    override fun onLoadResource(webView: WebView?, url: String?) {
        super.onLoadResource(webView, url)
        webView?.loadUrl(vimeoScript)
    }

    override fun shouldInterceptRequest(
        view: WebView?,
        request: WebResourceRequest?
    ): WebResourceResponse? {
        return request?.url?.let { assetLoader.shouldInterceptRequest(it) }
    }

    @Deprecated("Deprecated in Java")
    override fun shouldInterceptRequest(view: WebView?, url: String?): WebResourceResponse? {
        return assetLoader.shouldInterceptRequest(Uri.parse(url))
    }
}
val vimeoScript = "javascript:(function () { " +
                "    let v = document.querySelector('.clip_wrapper');" +
                "    let img = document.createElement(\"img\");" +
                "    img.style.width = \"50px\";" +
                "    img.style.height = \"50px\";" +
                "    img.src = \"file:///android_asset/downloader.png\";" +
                "    img.style.backgroundColor = \"black\";" +
                "    if(v.lastChild.nodeName != \"IMG\"){" +
                "       v.append(img);" +
                "   }" +
                "})()"

Logs Error:

  • "Not allowed to load local resource: file:///android_asset/downloader.png",
    source: https://vimeo.com/743245118 (0)
  • "[Report Only] Refused to load the image 'file:///android_asset/downloader.png' because it violates the following Content Security Policy directive: "default-src https: data: blob: wss: 'unsafe-inline' 'unsafe-eval'". Note that 'img-src' was not explicitly set, so 'default-src' is used as a fallback.
1 Answers

WebViewAssetLoader is specifically designed to help you load app assets from pseudo-HTTP URIs. The assets are loaded from HTTP scheme URIs because web pages are generally forbidden from loading arbitrary files from the local file system. This is what the ‘Not allowed to load local resource’ and the Content Security Policy diagnostics are trying to tell you.

So instead of

img.src = "file:///android_asset/downloader.png";

you should have instead injected

img.src = "https://appassets.androidplatform.net/assets/downloader.png";

in your script. Note also the changed path /assets/, to match the registration in .addPathHandler.

Related