Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open PDF in a WebView

I want to open a PDF in my WebView, and I found and combined codes on this forum.

But it catches the "No PDF application found" although I have multiple PDF apps installed, including Adobe Reader.

Here the code:

private class PsvWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);

            if (url.contains(".pdf")) {
                Uri path = Uri.parse(url); 
                Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
                pdfIntent.setDataAndType(path, "application/pdf");
                pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

                try
                {
                    startActivity(pdfIntent);
                }
                catch(ActivityNotFoundException e)
                {
                    Toast.makeText(PsvWebViewActivity.this, "No PDF application found", Toast.LENGTH_SHORT).show();
                }
                catch(Exception otherException)
                {
                    Toast.makeText(PsvWebViewActivity.this, "Unknown error", Toast.LENGTH_SHORT).show();
                }

            }

            return true;
        }   } }
like image 347
TheLD Avatar asked Feb 24 '12 16:02

TheLD


People also ask

How do I open a PDF in WebView?

Opening a PDF file in Android using WebView All you need to do is just put WebView in your layout and load the desired URL by using the webView. loadUrl() function. Now, run the application on your mobile phone and the PDF will be displayed on the screen.

Can we open PDF in WebView in Android?

We can load PDF in android easily using WebView. We will be using the simplest way for displaying PDF file in android. Using the Google Docs PDF viewer we could do this easily. Now lets get in to the implementation and source code.

How do I open PDF in WebView flutter?

If possible, you can display PDF documents and visit online websites using the add-in: import 'dart:html' as html; html. window. open('http:///www.website.com/document.pdf');


1 Answers

(1) Google Docs Viewer, You can open it in android Browser like,

mWebView.loadUrl("https://docs.google.com/gview?embedded=true&url="+ webUrl);

Update:

(2) Check this library, in build.gradle(app module) add this dependency,

compile 'com.github.barteksc:android-pdf-viewer:2.8.2'
like image 166
Samir Mangroliya Avatar answered Nov 12 '22 15:11

Samir Mangroliya