在WebView中打开PDF

问题描述 投票:12回答:6

我想在我的WebView中打开一个PDF,我在这个论坛上找到并组合了代码。

但是,虽然我安装了多个PDF应用程序,包括Adobe Reader,但它仍然可以找到“找不到PDF应用程序”。

这里的代码:

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;
        }   } }
android pdf android-intent toast webviewclient
6个回答
38
投票

(1)Google Docs Viewer,你可以在android浏览器中打开它,如,

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

更新:

(2)检查这个library,在build.gradle(app module)中添加这个依赖项,

compile 'com.github.barteksc:android-pdf-viewer:2.8.2'

11
投票

我知道,这个问题已经过时了。

但我真的很喜欢Xamarin使用Mozilla的pdf.js的方法。它适用于较旧的Android版本,您不需要特殊的PDF查看器应用程序,您可以轻松地在应用程序视图层次结构中显示PDF。

Git为此:https://mozilla.github.io/pdf.js/

其他选项:https://github.com/mozilla/pdf.js/wiki/Viewer-options

只需将pdfjs文件添加到Assets目录:

enter image description here

并按以下方式调用它:

// Assuming you got your pdf file:
File file = new File(Environment.getExternalStorageDirectory() + "/test.pdf");

webview = (WebView) findViewById(R.id.webview);
WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);
settings.setAllowFileAccessFromFileURLs(true);
settings.setAllowUniversalAccessFromFileURLs(true);
settings.setBuiltInZoomControls(true);
webview.setWebChromeClient(new WebChromeClient());
webview.loadUrl("file:///android_asset/pdfjs/web/viewer.html?file=" + file.getAbsolutePath());

很酷的事情:如果你想减少功能/控制的数量。转到Assets / pdfjs / web / viewer.html文件并将某些控件标记为隐藏。同

style="display: none;"

例如。如果您不喜欢正确的工具栏:

<div id="toolbarViewerRight" style="display: none;">...</div>

1
投票

更新:如果OS> = Marshmallow(API 23),这将打开内置应用程序(PDF Viewer)以显示pdf文件,否则将打开浏览器以使用“docs.google.com”服务器显示pdf。

WebView webView = new WebView(this);

    String url = "http://www.pdf995.com/samples/pdf.pdf"; 

    // Support all types of OS versions.
    if(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
        webView.loadUrl(url); // This will open it with a built-in PDF viewer app.
    else
        webView.loadUrl("http://docs.google.com/viewerng/viewer?url="+ url); // This will open it with a browser. On Android 5.0 (Api 21 - Lollipop) and bellow.

    // Set Download listener.
    webView.setDownloadListener((url1, userAgent, contentDisposition, mimetype, contentLength)
            -> startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url1))));

0
投票

旧网址无法使用此新网址

mWebView.loadUrl("http://drive.google.com/viewerng/viewer?embedded=true&url="+ webUrl);

我认为这有一个限制,所以更好的方法是直接启动pdf查看器应用程序的意图,如果没有应用程序启动pdf然后使用webview。


0
投票

这项工作对我来说

webView.loadUrl("https://docs.google.com/viewer?url=" + "url of your pdf"); 

0
投票

你可以试试这个。它适用于.pdf和.docx

 String pdfUrl = "https://www.abcd.com/assets/uploads/profilepics/abc.pdf"; //your pdf url
 String url = "http://docs.google.com/gview?embedded=true&url=" + pdfUrl;

 WebView webView = findViewById(R.id.webview_cv_viewer);
 webView.getSettings().setSupportZoom(true);
 webView.getSettings().setJavaScriptEnabled(true);
 webView.loadUrl(url);
© www.soinside.com 2019 - 2024. All rights reserved.