[英]How to open pdf file with in our app android+phonegap+JQM?
[英]Phonegap how to download and open pdf file in android app
最近我正在开发一个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');
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";
}
}
[更新2]我在Web控制台中遇到以下错误:Uncaught ReferrenceError:LocalFileSystem未在...中定义
可能是什么问题?
我遇到了与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
cordova plugin add https://github.com/don/FileOpener.git
window.plugins.fileOpener.open("file:///sdcard/Android/data/com.example.application/document.doc")
或iOS上的适当路径。 而已! 祝好运!
确保你的manifest.xml中有这些行:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
你可以在这里找到帮助如何在Android下载pdf文件?
确保
你应该安装git并安装文件插件(cordova 3.0+)
文件和文件传输插件
$ cordova plugin add org.apache.cordova.file $ cordova plugin add org.apache.cordova.file-transfer
PS。 试试插件fileOpener2
尝试使用以下步骤从URL打开任何类型的文档:
使用此代码:
handleDocumentWithURL(function(){console.log('success');},function(error){console.log('failure'); if(error == 53){console.log('没有处理此文件的app type。');}},' http://www.example.com/path/to/document.pdf ');
它适用于Android和IOS。 我用它来打开图像和PDF文件。
Android :它使用系统应用程序打开文件(如果可用),否则会出错,您可以处理。
IOS :它使用“完成”按钮和“ 选项 ”按钮以弹出窗口打开文件。
它不会显示您的文档网址。
来源可在此处获取: https : //github.com/ti8m/DocumentHandler
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.