Android上的Cordova没有打开PDF链接(并且没有显示“Open in ...”对话框)

时间:2016-05-18 12:44:06

标签: android cordova pdf

我有一个带有InAppBrowser和Whitelist插件的Cordova应用程序。遗憾的是,PDF文件的链接不会加载到webview中,并且用户看不到“打开文件...”对话框以使用系统应用程序打开此文件

以下是我的config.xml的一些值:

<access origin="*" />
<access launch-external="yes" origin="whatsapp:*"/>
<allow-navigation href="http://*/*" />
<allow-navigation href="https://*/*" />
<allow-intent href="tel:*" />
<allow-intent href="sms:*" />
<allow-intent href="mailto:*" />
<allow-intent href="geo:*" />

据我所知,Android不支持在webview中显示PDF文件。但是,如何触发Cordova显示“打开文件...(已安装的系统应用程序)”对话框?

3 个答案:

答案 0 :(得分:0)

对于Android,您可以先在本地下载

$http.get(url, {
 responseType: 'blob'
})
.success(function (blob) {
  $cordovaFile.writeFile(path, filename, blob, true)
    .then(function (file) {
      // open the file
      $cordovaFileOpener2.open(file.nativeURL.split('file://').pop(), 'application/pdf');
    });
});

如果已经设置,它将在Android上触发打开或打开默认的pdf查看器。您可以添加一些文件检查以确保它已下载。 $ cordovaFileTransfer现在可以正常工作,但在我尝试使用它时,它无法正常工作。

答案 1 :(得分:0)

我在Android Code中找到了一个可行的解决方案。在onCreate中设置WebViewClient并检查单击了哪个URL。对于PDF-Link,我将其发送到系统默认应用程序 如果链接是PDF,则进行非常基本的检查。可以改进。

    WebView cordovaView = (WebView) this.appView.getView();

    cordovaView.setWebViewClient(new SystemWebViewClient((SystemWebViewEngine) this.appView.getEngine()){
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url.contains(".pdf"))
            {
                Intent i = new Intent(Intent.ACTION_VIEW);
                i.setData(Uri.parse(url));
                startActivity(i);
            }

            return true;
        }
    });

答案 2 :(得分:0)

这可能对您有所帮助

url = "https://docs.google.com/gview?embedded=true&url="+YOUR_PDF_LINK;
var documentLink = cordova.InAppBrowser.open(url, '_blank', 'location=no,hardwareback=yes');

documentLink.addEventListener('loadstart', function(event) {

});

documentLink.addEventListener('exit', function(event) {
    documentLink.removeEventListener("loadstart", function(e){});
});