如何在phonegap中的图库中保存图像

时间:2016-03-16 20:04:27

标签: javascript android cordova

在我的应用中,我遇到了问题。

我可以使用下面的代码在图库中保存图像。但是当我构建apk并将其安装在手机上时,我无法在手机上保存图像。为什么会这样?



//---------------- 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>
&#13;
&#13;
&#13;

0 个答案:

没有答案