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
1 Answer
Reset to default 0For 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())
}
}