2

I have been using phonegap and am trying to download wallpaper jpgs to the device camera roll. I have been testing this on the mobile phonegap app connecting to phonegap server. The code I am using goes as follows:

function DownloadFile(URL, Folder_Name, File_Name) {
   if (URL == null && Folder_Name == null && File_Name == null) {
      return;
   }
   else {
      download(URL, Folder_Name, File_Name); //If available download function
   }
}

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.toURL(); // Returns Fulpath of local directory

   fp = fp + Folder_Name +"/"+ File_Name + ".jpg"; // fullpath and name of the file which we want to give
   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) {
                    console.log("cdvfile://localhost/persistent/" + fp);
                    alert("download complete: " + entry.fullPath.slice(1));
                },
             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);
             }
        );
   }

What is interesting is that when I click my button that fires these methods I receive an alert saying download complete: file_name. However, there never appears an image in my camera roll. Is this an iOS issue? I've tried looking at this natively as opposed to simulating it with phonegap serve and it makes no difference.

I've also tried a file_path like so "cdvfile://localhost/persistent/"+Folder_name+"/"+ File_name+"."+ext; with no different results.

In a difference post http://community.phonegap.com/nitobi/topics/file_download_not_working_properly_on_ios the answer for iOS was to use

<access origin="*" />

I made sure to use that in my config.xml file.

Does anyone have an idea as to what I am missing? This code has worked for others and oddly it states it completes correctly.

peterpod
  • 246
  • 1
  • 3
  • 11
  • Has anyone come up with a solution? My code looks correct and others have gotten it working but something is off – peterpod Apr 28 '15 at 15:27
  • 2
    I believe your code is fine for storing an image (or any file) to the _app's_ filesystem. I don't believe cordova.fileTransfer will ever be able to write a photo to your camera roll. For that purpose, try this plug-in: https://github.com/driftyco/cordova-camera-roll – jorisw Sep 23 '15 at 13:17

0 Answers0