我刚刚开始为 FirefoxOS 编码,并试图获取目录中的文件列表。
这个想法是找到每个文件的名称并将其添加到数组中(这可行),但我想返回填充的数组,这就是我遇到的问题。似乎该数组在函数期间被填充(因为我可以让它从中吐出文件名)但是当我想将它返回给另一个函数时它似乎是空的?
这是有问题的功能:
function getImageFromDevice (){
var imageHolder = new Array();
var pics = navigator.getDeviceStorage('pictures');
// Let's browse all the images available
var cursor = pics.enumerate();
var imageList = new Array();
var count = 0;
cursor.onsuccess = function () {
var file = this.result;
console.log("File found: " + file.name);
count = count +1;
// Once we found a file we check if there are other results
if (!this.done) {
imageHolder[count] = file.name;
// Then we move to the next result, which call the cursor
// success with the next file as result.
this.continue();
}
console.log("file in array: "+ imageHolder[count]);
// this shows the filename
}
cursor.onerror = function () {
console.warn("No file found: " + this.error);
}
return imageHolder;
}
谢谢你的帮助!