PhoneGap的如何下载和Android应用程序打开PDF文件 [英] Phonegap how to download and open pdf file in android app
问题描述
最近我工作的一个Android应用程序建立在PhoneGap的,我很新与移动的发展,这我SMY第一appsbut,和我被困在尝试下载一些文件(PDF,DOC,JPG格式)到本地存储和打开它,通过谷歌搜索后,我试图按照<一个href="http://stackoverflow.com/questions/13375234/how-to-open-downloaded-file-pdf-zip-docx-etc-in-phonegap-app">this解决方案。
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.
我跟着确切code的例子,下面是我如何调用该插件:
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是我的远程文件,当我执行它,我得到错误:类型错误:window.plugins是不确定的。任何人的帮助是AP preciated。
The url is my remote file, when i execute it, i got error: "TypeError: window.plugins is undefined". Anyone help is appreciated.
[更新] 我的$ C $ showPdf功能的C:
[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控制台错误:未捕获ReferrenceError:LocalFileSystem不是在...定义
[Update 2] I got below error in web console: Uncaught ReferrenceError: LocalFileSystem is not defined at ...
可能是什么问题?
推荐答案
我有同样的问题,因为OP而有很多不成功的方法后,我来到了以下解决方案(与科尔多瓦3.1.0-3.3.0和放大器测试;三星银河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):
首先,获得由文件请求的文件系统,然后从下载你的服务器,<一个href="http://docs.phonegap.com/en/3.1.0/cordova_file_file.md.html#FileTransfer_download">fileTransfer.download().在下载完成呼叫FileOpener后(这将打开一个弹出,从应用程序,如Adobe Reader的选择)。注意:确保通过CLI /终端包括了文件传输功能:科尔多瓦插件添加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 /终端并键入:
科尔多瓦插件添加https://github.com/don/FileOpener.git$c$c>
- 在成功下载和存储文件的设备上(见上文)你打开它后: <$c$c>window.plugins.fileOpener.open("file:///sdcard/Android/data/com.example.application/document.doc")$c$c>或iOS上适当的路径。
这就是它!祝你好运!
这篇关于PhoneGap的如何下载和Android应用程序打开PDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!