Webview does not load PDF files when clicking a link

I developed a web application that displays a list of pdf documents hosted on a web server. This application is built into the webview application for Android, however, when I download the application to my phone, selecting a pdf link does nothing. What am I doing wrong? Thanks

Here is the Java code:

package com.hellowebview; import android.app.Activity; import android.os.Bundle; import android.view.KeyEvent; import android.webkit.WebView; import android.webkit.WebViewClient; public class HellowebviewActivity extends Activity { /** Called when the activity is first created. */ private WebView mWebView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mWebView = (WebView) findViewById(R.id.webview); mWebView.getSettings().setJavaScriptEnabled(true); mWebView.loadUrl("http://aipnz.webs.com"); mWebView.setWebViewClient(new HelloWebViewClient()); mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY); } private class HelloWebViewClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView webview, String url) { webview.loadUrl(url); return true; } } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) { mWebView.goBack(); return true; } return super.onKeyDown(keyCode, event); } } 
+7
source share
7 answers

If you want to download pdf, you can use Google docs to download it.

 String googleDocs = "https://docs.google.com/viewer?url="; String pdf_url = "http://kronox.org/documentacion/Hello.Android.new.pdf"; webView.loadUrl(googleDocs + pdf_url); 

NOTE. You need android .permission.INTERNET in manifest file

+16
source

Just create an intent in the shouldOverrideUrlLoading method:

  @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { if ( urlIsPDF(url)){ Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.parse(url), "application/pdf"); try{ view.getContext().startActivity(intent); } catch (ActivityNotFoundException e) { //user does not have a pdf viewer installed } } else { webview.loadUrl(url); } return true; } 

And then whenever a user clicks a PDF link on your web browser page, the file opens in an external PDF application.

+13
source

This is the solution I am using:

 @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { if (url.endsWith(".pdf")){ String pdfUrl = googleDocs + url; view.loadUrl(pdfUrl); } else { view.loadUrl(url); } return true; } 

from

 private final String googleDocs = "https://docs.google.com/viewer?url="; 
+5
source
 webView.setWebViewClient(new WebViewClient() { public boolean shouldOverrideUrlLoading(WebView view, String url) { if (url.endsWith(".pdf")) { Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.parse(url), "application/pdf"); try { view.getContext().startActivity(intent); } catch (ActivityNotFoundException e) { //user does not have a pdf viewer installed } } else { webView.loadUrl(url); } return true; } } 
+5
source

You need to override the shouldOverrideUrlLoading method in the WebClient. I use this approach with a combination of intent and Google Docs as a backup:

 /* You might want to move this string definition somewhere else */ final String googleDocs = "https://docs.google.com/viewer?url="; WebView webView = new WebView(getContext()); //webView.getSettings().setJavaScriptEnabled(true); webView.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView webView, String url) { if (url.endsWith(".pdf")) { Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.parse(url), "application/pdf"); /* Check if there is any application capable to process PDF file. */ if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } else { /* If not, show PDF in Google Docs instead. */ view.loadUrl(googleDocs + url); } } else { webView.loadUrl(url); } return true; } }); 

You may need to change the context transfer and access the startActivity method, but other than that it should work as it is.

Also note that with API 24 there are 2 shouldOverrideUrlLoading methods that you can override. As pointed out here from @CommonsWare, it is ok to override the deprecated method.

+4
source

use loadurl another overloaded method ... and write internet permission

0
source

Sharing this answer when I tried all the above solutions, but I didn’t succeed, this is Kotlin code (not javascript)

  myWebView.setWebViewClient(object:WebViewClient() { override fun shouldOverrideUrlLoading(view:WebView, url:String):Boolean { if (url.endsWith(".pdf")) { val intent = Intent(Intent.ACTION_VIEW) intent.setDataAndType(android.net.Uri.parse(url), "application/pdf") try { view.getContext().startActivity(intent) } catch (e:ActivityNotFoundException) { //user does not have a pdf viewer installed } } else { myWebView.loadUrl(url) } return true } }) 
0
source

Source: https://habr.com/ru/post/907638/


All Articles