简体   繁体   中英

Android WebView loadDataWithBaseURL shouldInterceptRequest

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?

Despite the doc says this:

This callback is invoked for a variety of URL schemes (eg, 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM