Phonegap如何在Android应用程序中下载和打开pdf文件 [英] Phonegap how to download and open pdf file in android app

查看:29
本文介绍了Phonegap如何在Android应用程序中下载和打开pdf文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

recently i am working on a android apps build in phonegap, i am quite new with mobile development and this i smy first appsbut, and i am stuck when try to download some documents (pdf, doc, jpg) to local storage and open it, after search through google and i tried to follow this solution.

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

I followed the exact code in the example and below is how i call the plugins:

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

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

The url is my remote file, when i execute it, i got error: "TypeError: window.plugins is undefined". Anyone help is appreciated.

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

[update] My code of showPdf function:

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 未定义于 ...

[Update 2] I got below error in web console: Uncaught ReferrenceError: LocalFileSystem is not defined at ...

可能是什么问题?

推荐答案

我遇到了和 OP 一样的问题,很多不成功的方法后来我得出了以下解决方案(用 Cordova 3.1.0-3.3.0 & Samsung 测试Galaxy S3 和 iOS 7):

I had the same problem as the OP and many unsuccessful approaches later I came to the following solution (tested with Cordova 3.1.0-3.3.0 & Samsung Galaxy S3 & iOS 7):

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

First, get the file by requesting the file system and then download it from your server with fileTransfer.download(). After completing the download call the FileOpener (which will open a pop up where to choose from apps like Adobe Reader). Note: Make sure to include the FileTransfer feature via CLI/Terminal: 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 上的相应路径.

就是这样!祝你好运!

这篇关于Phonegap如何在Android应用程序中下载和打开pdf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆