0

In my app I have a problem.

I am able to save image in gallery in using the code below. But when I build apk and install it on my phone, I can't save images on the phone. Why is it so?

//---------------- save.js file ----------------

 

//First step check parameters mismatch and checking network connection if available call    download function
    function DownloadFile(URL, Folder_Name, File_Name) {
      //Parameters mismatch check
      if (URL == null && Folder_Name == null && File_Name == null) {
          return;
      }
      else {
          //checking Internet connection availablity
          var networkState = navigator.connection.type;
          if (networkState == Connection.NONE) {
              return;
          } else {
              download(URL, Folder_Name, File_Name); //If available download function call
          }
        }
      }

    function download(URL, Folder_Name, File_Name) {
        //step to request a file system 
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, fileSystemSuccess, fileSystemFail);

        function fileSystemSuccess(fileSystem) {
            var download_link = encodeURI(URL);
            ext = download_link.substr(download_link.lastIndexOf('.') + 1); //Get extension of URL

            var directoryEntry = fileSystem.root; // to get root path of directory
            directoryEntry.getDirectory(Folder_Name, { create: true, exclusive: false }, onDirectorySuccess, onDirectoryFail); // creating folder in sdcard
            var rootdir = fileSystem.root;
         
            // var fp = rootdir.fullPath; // Returns Fulpath of local directory
         var fp = rootdir.toURL(); 
            // fp = fp + "/" + Folder_Name + "/" + File_Name + "." + ext; // fullpath and name of the file which we want to give
            fp =  "file:///mnt/sdcard/download/" + File_Name + "." + "png"; // fullpath and name of the file which we want to give
            
         // download function call
            filetransfer(download_link, fp);
        }

        function onDirectorySuccess(parent) {
            // Directory created successfuly
        }

        function onDirectoryFail(error) {
            //Error while creating directory
            //alert("Unable to create new directory: " + error.code);
        }

          function fileSystemFail(evt) {
            //Unable to access file system
            alert(evt.target.error.code);
         }
        }


        function filetransfer(download_link, fp) {
        var fileTransfer = new FileTransfer();
        // File download function with URL and local path
        fileTransfer.download(download_link, fp,
                            function (entry) {
                                alert("is done" + entry.fullPath);
                            },
                         function (error) {
                             //Download abort errors or download failed errors
                             alert("download error source " + error.source);
                             //alert("download error target " + error.target);
                             //alert("upload error code" + error.code);
                         }
                    );
        }
    }
    <html><head>
     
        <script type="text/javascript" src="cordova.js"></script>  
        <script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
        <!--or use jquery-2.1.3.min.j-->
      
      <script src="save.js"></script>
        <script>
            $( document ).ready(function(){
              
              $('#btn').click(function(){
                 var res = confirm("are you sure?");
               if (res== false) 
                return false;
                
               var d = new Date(); 
               var urlAddress=$('#fish-pic').attr('src');
               DownloadFile(urlAddress, "", "fish_"+d.getTime());
              });

            });
              
       </script> 
      </head>
      <body>
      <img id="fish-pic" src="http://i.imgur.com/KGrV41o.png" />
      <button id="btn">save</button>
  </body></html>
Nick Volynkin
  • 14,023
  • 6
  • 43
  • 67
DMAON
  • 1
  • in `config.xml` do you have a domain whitelist? Example: `` – Dawson Loudon Mar 16 '16 at 20:40
  • yes i did , but not worked! – DMAON Mar 17 '16 at 09:09
  • Check for the bellow two link hope it will help you... http://stackoverflow.com/questions/21577230/phonegap-save-image-from-url-into-device-photo-gallery http://stackoverflow.com/questions/29288684/phonegap-save-base64-image-to-gallery – Nemo Mar 18 '16 at 18:02

0 Answers0