kotlin - How to load resources coming from the webview in android? - Stack Overflow

Opening PDFs coming from another web link. PDFs are supposed to be downloaded from the link and then an

Opening PDFs coming from another web link. PDFs are supposed to be downloaded from the link and then an external app is used to open to PDF. This applies for all the resources.

.xlsx .pdf .jpeg

Opening PDFs coming from another web link. PDFs are supposed to be downloaded from the link and then an external app is used to open to PDF. This applies for all the resources.

.xlsx .pdf .jpeg

Share Improve this question asked Mar 17 at 9:44 AnglesvarAnglesvar 1,1549 silver badges18 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

For this, we need to understand what Android does behind the picture. When we open a web view through WebViewClient it considers everything as an unrelated resource. If we want to open a PDF from the web view itself, we can override onLoadResource.

class CustomWebViewClient: WebViewClient()
{
    override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean
    {
        if (request == null)
        {
            return false
        }

        view!!.settings.allowFileAccess = true
        view.settings.allowContentAccess = true
        view.settings.domStorageEnabled = true
        view.settings.javaScriptEnabled = true
        view.settings.loadWithOverviewMode = true

        view.loadUrl(request.url.toString())

        return false
    }

    override fun onLoadResource(view: WebView?, url: String?) {
        super.onLoadResource(view, url)
        if (url!!.contains(".pdf")) {
            WebHelper.openBrowser(url.toUri())
        }
    }

    override fun onReceivedError(
        view: WebView?,
        request: WebResourceRequest?,
        error: WebResourceError?
    ) {
        super.onReceivedError(view, request, error)
        Log.e("error", error.toString())
    }
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744569207a4581355.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信