7

最近我正在开发一个在phonegap中构建的android应用程序,我对移动开发很陌生,这是我的第一个应用程序但是,当我尝试将一些文档(pdf,doc,jpg)下载到本地存储并打开它时,我被卡住了,通过谷歌搜索后,我试图遵循这个解决方案

我遵循示例中的确切代码,以下是我调用插件的方式:

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

该 url 是我的远程文件,当我执行它时,出现错误:“TypeError:window.plugins 未定义”。任何帮助表示赞赏。

[更新]我的 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";
    }

}

[更新 2]我在 Web 控制台中收到以下错误:未捕获的引用错误:LocalFileSystem 未定义在 ...

可能是什么问题?

4

4 回答 4

13

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

解决方案

首先,通过请求文件系统获取文件,然后使用fileTransfer.download()从您的服务器下载它。完成下载后调用 FileOpener(它将打开一个弹出窗口,您可以在其中选择 Adob​​e Reader 等应用程序)。注意:确保通过 CLI/终端包含 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 上的适当路径打开它。

而已!祝你好运!

于 2014-01-07T06:31:58.937 回答
0

确保您的 manifest.xml 中有这些行:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

您可以在此处找到帮助如何在 Android 中下载 pdf 文件?

于 2013-06-24T08:44:16.647 回答
0

确保

您应该安装 git 并安装文件插件(cordova 3.0+)

文件和文件传输插件

$ cordova plugin add org.apache.cordova.file $ cordova plugin add org.apache.cordova.file-transfer

附言。尝试插件 fileOpener2

https://build.phonegap.com/plugins/1117

于 2014-10-30T03:11:49.070 回答
0

尝试使用以下步骤从 URL 打开任何类型的文档:

它适用于我在 Android 和 IOS 上。我用它来打开图像PDF文件。

Android:如果可用,它会使用系统应用程序打开文件,否则会给出错误,您可以处理。

IOS:它在弹出窗口中打开文件,如使用完成按钮和选项按钮的视图。

它不显示您的文档 URL。

源代码在这里:https ://github.com/ti8m/DocumentHandler

于 2015-11-16T10:07:36.433 回答