16

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

但是,尽管我安装了多个 PDF 应用程序,包括 Adob​​e 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;
        }   } }
4

7 回答 7

43

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

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

更新:

(2)勾选这个,在build.gradle(app模块)中添加这个依赖,

compile 'com.github.barteksc:android-pdf-viewer:2.8.2'
于 2012-02-24T17:04:45.210 回答
14

我知道,这个问题很老了。

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

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

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

只需将 pdfjs 文件添加到您的资产目录:

在此处输入图像描述

并通过以下方式调用它:

// 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>
于 2017-03-29T08:02:25.790 回答
3

更新:如果 OS >= Marshmallow (API 23),这将打开内置应用程序(PDF 查看器)以显示 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))));
于 2018-12-27T06:33:57.097 回答
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);
于 2019-01-31T11:10:48.623 回答
0

如果您尝试查看具有 URL 的 PDF,并且您没有访问需要身份验证的内容,那么您最好的选择是:

val webPage = Uri.parse(customizedUrl) //This is optional, based on how your URL is provided
val intent = Intent(Intent.ACTION_VIEW, webPage)
startActivity(intent)

浏览器委托设备上的 PDF 查看器打开文档。

于 2021-01-28T20:32:25.477 回答
-1

旧网址不起作用使用这个新网址

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

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

于 2018-03-20T13:11:17.397 回答
-2

这对我有用

webView.loadUrl("https://docs.google.com/viewer?url=" + "url of your pdf"); 
于 2018-05-31T20:03:03.100 回答