我试图循环浏览android文件系统以查找任何图像。我写了下面的递归方法,但它不起作用。我尝试在递归中使用相同的延迟,但是我无法找到能够识别其递归结束并返回结果的条件。
getEntries: function(path, def, deepCount, results) {
var self = this;
var deferred = def || $q.defer();
var deepCount = deepCount || 0;
var results = results || [];
// $cordovaDialogs.alert("inside entries");
window.resolveLocalFileSystemURL(path, function(fileSystem) {
var directoryReader = fileSystem.createReader();
directoryReader.readEntries(function(entries) {
var arrayLength = entries.length;
// var log = [];
// angular.forEach(entries, function(value, key) {
// if(key.isDirectory )
// });
// $cordovaDialogs.alert("got entries");
for (var i = 0; i < arrayLength; i++) {
var file = entries[i];
// $cordovaDialogs.alert(file);
if(file.isDirectory){
deepCount += 1;
self.getEntries(file.nativeURL, deferred, deepCount, results);
}
else if (file.isFile){
//check if the file extension is of type image
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 results;
return deferred.promise;
},
答案 0 :(得分:0)
我最终遵循问题中给出的解决方案 - How to iterate through file system directories and files using javascript?
但是,我注意到我的方法存在的问题是,我在递归调用中调用了window.resolveLocalFileSystemURL,这导致了问题。我应该只是遍历目录。