Phonegap如何在Android应用程序中下载和打开pdf文件

Der*_*k.X 7 android cordova

最近我正在开发一个android应用程序内置的phonegap,我是一个很新的移动开发,这我smy第一个应用程序但是,当我尝试将一些文件(pdf,doc,jpg)下载到本地存储并打开它时,我陷入困境,通过谷歌搜索后,我试图遵循这个解决方案.

我按照示例中的确切代码,下面是我如何调用插件:

window.plugins.downloader.downloadFile(url,'/sdcard/download/', 'test.pdf', true, downloadOkCallbak, downloadErCallbak);
    window.plugins.pdfViewer.showPdf('/sdcard/download/test.pdf');
Run Code Online (Sandbox Code Playgroud)

url是我的远程文件,当我执行它时,我收到错误:"TypeError:window.plugins is undefined".任何帮助表示赞赏.

[更新]我的showPdf函数代码:

public String showPdf(String fileName) {

    File file = new File(fileName);

    if (file.exists()) {
        try {
            Uri path = Uri.fromFile(file);
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(path, "application/pdf");
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);


            //intent.setData(Uri.parse(fileName));
            this.ctx.startActivity(intent);
            return "";
        } catch (android.content.ActivityNotFoundException e) {
            System.out.println("PdfViewer: Error loading url "+fileName+":"+ e.toString());
            return e.toString();
        }            

    }else{
        return "file not found";
    }

}
Run Code Online (Sandbox Code Playgroud)

[更新2]我在Web控制台中遇到以下错误:Uncaught ReferrenceError:LocalFileSystem未在...中定义

可能是什么问题?

Omn*_*con 13

我遇到了与OP相同的问题和许多不成功的方法后来我来到以下解决方案(使用Cordova 3.1.0-3.3.0和三星Galaxy S3和iOS 7测试):

首先,通过请求文件系统获取文件,然后使用fileTransfer.download()从服务器下载它.完成下载后,调用FileOpener(将打开一个弹出窗口,从Adobe Reader等应用程序中进行选择).注意:确保通过CLI/Terminal包含FileTransfer功能:cordova plugin add org.apache.cordova.file

  • 导航到您的项目文件夹
  • 打开CLI /终端并输入: cordova plugin add https://github.com/don/FileOpener.git
  • 在设备上成功下载并存储文件后(见上文),您可以使用:window.plugins.fileOpener.open("file:///sdcard/Android/data/com.example.application/document.doc")或iOS上的相应路径打开它.

而已!祝好运!