I've been attempting to iterate through the Android file system in search of any images. I created the following recursive method, but it's not functioning as expected. I've been struggling to determine a condition that can identify the end of the recursion and return the results.
getEntries: function(path, def, deepCount, results) {
var self = this;
var deferred = def || $q.defer();
var deepCount = deepCount || 0;
var results = results || [];
window.resolveLocalFileSystemURL(path, function(fileSystem) {
var directoryReader = fileSystem.createReader();
directoryReader.readEntries(function(entries) {
var arrayLength = entries.length;
for (var i = 0; i < arrayLength; i++) {
var file = entries[i];
if(file.isDirectory){
deepCount += 1;
self.getEntries(file.nativeURL, deferred, deepCount, results);
}
else if (file.isFile){
if(file.name.match(/\.(jpg|jpeg|png|gif)$/)){
results.push(file.nativeURL);
}
}
}
if (!def && deepCount == 1 ) {
deferred.resolve(results);
}
else {
deepCount -= 1;
}
}, function(error) {
deferred.reject(error);
console.log(error);
});
}, function(error) {
deferred.reject(error);
console.log(error);
});
return deferred.promise;
},