2

I´m learning about files operations (save, open, etc) with cordova (and Intel XDK), I can save a pdf file following this solution: Trying to save a PDF file with Intel XDK

But now I want to know how can I open this very same saved pdf file. Can you show me a sample to achieve this (for Android) so I can apply it into my project?

What I want is that when the file is called then Android show me the "Open with..." window so I can choose if open with Adobe Reader or other app.

I´m checking the official documentation but I´ll need help to understand this part.

Thank you all.

Community
  • 1
  • 1
Alan Alvarez
  • 646
  • 2
  • 11
  • 32
  • Until now I´m trying the official documentation: https://github.com/apache/cordova-plugin-file but I´m having different issues (some with Intel xdk config). That´s why I need a simple working sample to be sure what problems belong to my project and what belong to the Intel xdk configuration. – Alan Alvarez Sep 15 '16 at 18:25
  • Finally I can do it splitting the problem to: *One button to create the document *One button to create the pdf content *One button to write the pdf content into the created document *One button to open the file *Only Cordova File plugin And following and adapting the solution in this post: https://github.com/pwlin/cordova-plugin-file-opener2/issues/60 Later I´ll do a video tutorial because this issue seems to be common and hard to solve. – Alan Alvarez Sep 25 '16 at 14:35

1 Answers1

0

Use this plugin Plugin Link.You can use following sample code:

cordova.plugins.fileOpener2.open(
                             yourfileURL, 
                             "application/pdf",
                            {
                                error : function(e){
                                }, 
                                success : function(){
                                } 
                            } 
                        );

Like following you can get the path of directory where the file stored.

function onSuccessFile(fileSystem) {
    alert(fileSystem.root.fullPath);
    alert(fileSystem.root.nativeURL);
}
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onSuccessFile, null);

So,to get the file path add the fileName with the path of that directory.

Other option is cordova.file.dataDirectory.It will return the path of that same directory.

Homen
  • 1,222
  • 1
  • 12
  • 16
  • Thanks, but I dont know what to put instead of "yourfileURL" because the Persistent and Temporary folders (where the pdf is stored) can change when I compile for Android. – Alan Alvarez Sep 15 '16 at 18:38
  • Do one thing store the pdf in a specific directory.Like temporary folder or some folder of internal memory.Then I can tell you the path.Otherwise, you can store the full path of the file in `sessionStorage` while saving the file and use that path when you open the file – Homen Sep 16 '16 at 05:48
  • Alright, lets say I´m saving into Temporary folder, what would be the path? Also, how can I store the full path with "sessionStorage"? If is possible to open with: window.open(somePath, '_system', 'location=yes'); Sorry if I´m being to noob. – Alan Alvarez Sep 16 '16 at 15:09
  • Check my answer.I added how you can get the path.And `window.open(somePath, '_system', 'location=yes')` is only use for `html` link – Homen Sep 16 '16 at 19:33
  • Thanks Homen now I can get the folder path but changing to fileSystem.root.toURL() because ...fullPath and ...nativeURL seems to be deprecated. Now I can save the pdf in the device but when I try to open with Adobe Reader or other options I got "The document have a not valid path" or "Can´t open the file" Which is strange because I put an alert to show the path and file and works (file:///data/data/xdk.intel.cordova.template.lite/cache/test.pdf) – Alan Alvarez Sep 16 '16 at 20:24
  • Welcum.Access the pdf file using file explorer.And try to open it using file explorer.Because there may be some problem while you creating the file – Homen Sep 17 '16 at 05:29
  • And you are using `cacheDirectory` directory.This directory has some restriction.So try to use `cordova.file.externalDataDirectory` directory – Homen Sep 17 '16 at 05:40
  • I´ll try but I saw that cordova.file.externalDataDirectory refers to external sd and the app must work without an external sd. Anyway I´ll post again my last doubt. Thanks for your help Homen – Alan Alvarez Sep 17 '16 at 16:32