Android WebView loadDataWithBaseURL shouldInterceptRequest

367 views Asked by At

I am using a WebView to display an HTML string with img tags. These tags must display pictures stored in a ZIP archive, so I need to intercept the images requests to return a ZipInputStream.

I'm thus using loadDataWithBaseURL and shouldInterceptRequest, but shouldInterceptRequest is never called for my pictures requests. Here's my WebViewClient client:

webView.webViewClient = object : WebViewClient() {

    override fun shouldInterceptRequest(view: WebView?, request: WebResourceRequest?): WebResourceResponse? {
        println("shouldInterceptRequest1 url: ${request?.url}")
        return super.shouldInterceptRequest(view, request)
    }

    override fun shouldInterceptRequest(view: WebView?, url: String?): WebResourceResponse? {

        println("shouldInterceptRequest2 url: $url")

        @Suppress("DEPRECATION")
        return super.shouldInterceptRequest(view, url)
    }
}

And here's what I do to load the HTML:

webView.loadDataWithBaseURL(null, "<div><img src='file://path/to/my/image.png'/></div>", "text/html", "UTF-8", null)

All I get in the logcat is this:

shouldInterceptRequest1 url: data:text/html;charset=utf-8;base64,

shouldInterceptRequest2 url: data:text/html;charset=utf-8;base64,

The img request does not trigger shouldInterceptRequest.

What's wrong?

1

There are 1 answers

0
Tim Autin On

Despite the doc says this:

This callback is invoked for a variety of URL schemes (e.g., http(s):, data:, file:, etc.), not only those schemes which send requests over the network. This is not called for javascript: URLs, blob: URLs, or for assets accessed via file:///android_asset/ or file:///android_res/ URLs.

In my very basic sample code shouldInterceptRequest isn't called for file:// URIs when using loadDataWithBaseURL... Using a http:// scheme works.