【问题标题】:How to open locally saved PDF file in an Android WebView?如何在 Android WebView 中打开本地保存的 PDF 文件?
【发布时间】:2014-08-20 14:29:45
【问题描述】:

我尝试在 WebView 中打开本地保存的 PDF 文件。我可以使用 IntentService 打开它,但我想在 WebView 中打开它。除了 PDF,所有其他文件都适合我。

我使用了以下代码:

WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setBuiltInZoomControls(true);
settings.setSupportZoom(true);
settings.setAllowFileAccess(true);
String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(mFilePath));
webView.loadData(mFilePath, mimeType, "UTF-8");

有人可以帮忙吗?提前致谢。

【问题讨论】:

  • WebView 没有显示 PDF 文件的内置功能。
  • 感谢您的评论。

标签: android pdf webview


【解决方案1】:

您可以使用 Google Docs Viewer 在线阅读您的 pdf: webview.getSettings().setJavaScriptEnabled(true); webview.loadUrl("http://docs.google.com/gview?embedded=true&url=" + pdf);

【讨论】:

    【解决方案2】:

    您也可以使用 iframe。

    pdfView.setWebChromeClient(new WebChromeClient());
    WebSettings settings = pdfView.getSettings();
    settings.setPluginState(WebSettings.PluginState.ON);
    pdfView.setWebViewClient(new WebViewClient());
    pdfView.setBackgroundColor(0x00000000);
    settings.setAllowFileAccess(false);
    settings.setBuiltInZoomControls(true);
    settings.setSupportZoom(true);
    settings.setUseWideViewPort(true);
    settings.setLoadWithOverviewMode(false);
    settings.setSaveFormData(false);
    settings.setJavaScriptEnabled(true);
    
    String html = "<iframe src=\"http://docs.google.com/gview?url=" + 
        path + "&embedded=true&wmode=opaque\"" +
        "style=\"width:600px; height:900px;\" scrolling=\"no\"" + 
        "frameborder=\"0\" allowfullscreen></iframe>";
    
    pdfView.loadDataWithBaseURL(null, html, "text/html", "UTF-8", null);
    

    【讨论】:

    • $path 是指向本地 pdf 文件/资源​​的 URI,还是指向 Web pdf 文件/资源​​?
    猜你喜欢
    • 2012-07-21
    • 2018-08-19
    • 1970-01-01
    • 1970-01-01
    • 2018-01-23
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 2013-10-07
    相关资源
    最近更新 更多