Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
838 views
in Technique[技术] by (71.8m points)

cordova - Phonegap how to download and open pdf file in android app

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');

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

[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";
    }

}

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

What could be the issue?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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):

Solution

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

  • Navigate to your project folder
  • Open CLI/Terminal and type in: cordova plugin add https://github.com/don/FileOpener.git
  • After successful download and storage of your file on the device (see above) you open it with: window.plugins.fileOpener.open("file:///sdcard/Android/data/com.example.application/document.doc") or the appropriate path on iOS.

That's it! Good luck!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...