I have found that the replace attribute in the createDir method is not obeyed. When set to true and the file already exists I get error code 12 (PATH_EXISTS_ERR).
$cordovaFile.createDir("test", true).then(function (result) {
console.log("Directory Created");
}, function(err) {
//This line is being called when the file already exists
console.log("Error creating directory: "+err);
});
If anyone has an idea as to a work around that would be great.
David
To be thorough, I have specified the following in the config.xml in order to specify the default file locations.
<preference name="AndroidPersistentFileLocation" value="Internal" />
<preference name="iosPersistentFileLocation" value="Library" />
I met the same problem. I modifying createDir method within ng-cordova.js. like this:
createDir: function (dir, replaceBOOL) {
var q = $q.defer();
getFilesystem().then(
function (filesystem) {
filesystem.root.getDirectory(dir, {
create: true,
exclusive: replaceBOOL
},
function () {
q.resolve();
},
function () {
q.reject();
});
}
);
return q.promise;
},
It's Solved.
Here is the work around that I came up with, it's unfortunate that this is necessary.
First add removeDir function to ng-cordova.js
removeDir: function(dir) {
var defer = $q.defer();
getDirectory(dir, {create: false}).then(
function(entry) {
entry.removeRecursively(
function(result) {
defer.resolve();
},
function(error) {
console.log("Error removing directory: "+error.code);
defer.reject(error);
}
);
},
function(error) {
console.log("Error retrieving directory - "+dir+" : "+error.code);
defer.reject(error);
}
);
return defer.promise;
},
Then check for the directory and remove it, if it is there, else just create the directory
$cordovaFile.checkDir("test").then(
function(){
console.log("Directory Exists");
$cordovaFile.removeDir("test").then(
function(){
createDirectory();
}
)
},function() {
console.log("Directory Doesn't Exist");
createDirectory();
}
);
function createDirectory() {
$cordovaFile.createDir("queuedItems/images/" + sightingId, true).then(function (result) {
console.log("Directory Created");
}, function (err) {
console.log("Could not create directory for sighting");
});
}