Wednesday, April 2, 2014

Create a Cache folder in the Project's root folder in a PhoneGap/Cordova Project.

When getting application root we have to use to different methods in different platforms. That’s because in Android you can access any folder and when called for the persistent storage root, from your application it will returns the root of the memory card or phone's persistent memory. But in iOS, when you called for persistent storage root from your application (like the code shown in below), it will returns the ‘Documents’ folder which is located right at where your app is installed in the phone memory.

Code:


var root;
var imageCache;           

//get the root of the application.
function getAppRoot() {

  if (device.platform == "Android") {
window.resolveLocalFileSystemURI("file:///data/data/com.geveo.myapp", onSuccess, onError);
        
  } else if (device.platform == "iOS") {
 window.requestFileSystem(LocalFileSystem.PERSISTENT,0, function(fileSystem) {
                                 root = fileSystem.root;
                                 onSuccess(root);
                                 }, function(evt) { 
                                   // error getting file system
                                   console.log(evt.target.error.code);
                                 });
}
}

//create cache if not exist iside root folder
function onSuccess(entry) {
root = entry;
root.getDirectory("ImageCache", {
                      create : true,
                      exclusive : false
                      }, onImageCacheCreateSuccess, onImageCacheCreateFail);
}

function onImageCacheCreateSuccess(dir) {
    
 imageCache = dir;
 console.log(dir.fullPath.toString());
}

function onImageCacheCreateFail(error) {
    console.log("ImageCache Create Fail"+error.code );
    
}

function onError(error) {
console.log("Error"+error.code );
}


No comments:

Post a Comment