7 Replies Latest reply on Oct 19, 2016 6:06 AM by sfelaco.neg

    Open a file in asset application directory

    sfelaco.neg Level 1

      I'm working with Phonegap 6.3.3. I want open a PDF file stored in application directory. I'm using FileOpener plugin.

       

       

      function openPDF(event){
         try {
      
         var filePath = cordova.file.applicationDirectory + "www/assets/pdf/pdf_1.pdf";
        console.log("FILEPATH: " + filePath);
        window.plugins.fileOpener.open(filePath);
         } catch(err) {
        console.log(err.message);
         }
      
      
      
      
      

      In android device the pdf doesn't open the file (File not found). Can you help me please?

        • 1. Re: Open a file in asset application directory
          simon.macdonald Adobe Employee

          The file opener plugin cannot open a file from the assets folder of your app. You would have to copy the pdf to the /sdcard of your Android device for the PDF viewing app to have access to it.

           

          There is an excellent answer for this on Stack Overflow

          1 person found this helpful
          • 2. Re: Open a file in asset application directory
            sfelaco.neg Level 1

            Thansk, I'm using the Asset2SD for the copy:

             

            asset2sd.copyFile({
                      asset_file: "www/assets/pdf/pdf_1.pdf",
                      destination_file: "sdcard/Download/pdf_1.pdf"
                  },
                  function() {console.log("File copied: pdf_1.pdf"); },
                  function() { console.log("Failed File copied: pdf_1.pdf"); }
                );
                var filePath = "file:///sdcard/Download/pdf_1.pdf"
                console.log("FILEPATH: " + filePath);
                window.plugins.fileOpener.open(filePath);
            

             

            I have this error:

            E/Asset2SD: Error occurred while copying file: Unable to create directory
            10-18 15:12:46.201 28808-28871/com.sky.skyapp W/CordovaPlugin: Attempted to send a second callback for ID: Asset2SD634203703
            Result was: "Invalid action"
            

             

            What is the problem?

            • 3. Re: Open a file in asset application directory
              simon.macdonald Adobe Employee

              Pretty you need the `/` in front of `sdcard`.

               

              `destination_file: "/sdcard/Download/pdf_1.pdf"`.

              • 4. Re: Open a file in asset application directory
                sfelaco.neg Level 1

                Now I have this message error:

                Error occurred while copying file: /storage/emulated/0/Download/pdf_1.pdf: open failed: EACCES (Permission denied)

                • 5. Re: Open a file in asset application directory
                  simon.macdonald Adobe Employee

                  Does your app include the write external storage permission? If it does are you sure that "Download" exists?

                  • 6. Re: Open a file in asset application directory
                    sfelaco.neg Level 1

                    Downlod exists. This is permission in my config.xml file

                     

                    <access origin="*" />
                        <allow-navigation href="http://*/*" />
                        <allow-navigation href="data:*" />
                        <allow-intent href="http://*/*" />
                        <allow-intent href="https://*/*" />
                        <allow-intent href="tel:*" />
                        <allow-intent href="sms:*" />
                        <allow-intent href="mailto:*" />
                        <allow-intent href="geo:*" />
                        <platform name="android">
                            <allow-intent href="market:*" />
                        </platform>
                        <platform name="ios">
                            <allow-intent href="itms:*" />
                            <allow-intent href="itms-apps:*" />
                        </platform>
                    
                    • 7. Re: Open a file in asset application directory
                      sfelaco.neg Level 1

                      I tried to copy in cache directory with this code:

                       

                       

                        var srcFile = cordova.file.applicationDirectory + "www/assets/pdf/pdf1.pdf";
                        console.log("FILEPATH: " + srcFile);
                        window.resolveLocalFileSystemURL(srcFile,
                        function(entry){
                        console.log("File " + entry.fullPath);
                      
                        console.log("DEST FOLDER: " + cordova.file.cacheDirectory");
                        var destDir = new DirectoryEntry({fullPath: cordova.file.cacheDirectory});
                      
                        entry.copyTo(
                        destDir,
                        "pdf1.pdf",
                      
                        function(entry) { 
                        console.log("Success: " + entry.fullPath);
                        ....
                        },
                      
                        function(error) {
                        console.log("Fail!");
                        console.log(error.code);
                        }
                        );
                      
                        },
                        function(evt){
                        console.log(" resolveLocalFileSystemURI evt"+ evt.message);
                        }
                        );

                       

                      In android Studio console I have this exception with error code 1000.

                       

                      10-19 14:57:41.509 23645-25346/com.my.myapp W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'org.json.JSONObject org.apache.cordova.file.Filesystem.copyFileToURL(org.apache.cordova.file.LocalFilesystemURL, java.lang.String, org.apache.cordova.file.Filesystem, org.apache.cordova.file.LocalFilesystemURL, boolean)' on a null object reference
                      10-19 14:57:41.509 23645-25346/com.my.myapp W/System.err:     at org.apache.cordova.file.FileUtils.transferTo(FileUtils.java:796)
                      10-19 14:57:41.509 23645-25346/com.my.myapp W/System.err:     at org.apache.cordova.file.FileUtils.access$1300(FileUtils.java:56)
                      10-19 14:57:41.510 23645-25346/com.my.myapp W/System.err:     at org.apache.cordova.file.FileUtils$22.run(FileUtils.java:521)
                      10-19 14:57:41.510 23645-25346/com.my.myapp W/System.err:     at org.apache.cordova.file.FileUtils$25.run(FileUtils.java:663)
                      10-19 14:57:41.510 23645-25346/com.my.myapp W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
                      10-19 14:57:41.510 23645-25346/com.my.myapp W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
                      10-19 14:57:41.510 23645-25346/com.my.myapp W/System.err:     at java.lang.Thread.run(Thread.java:833)