01-01-2013 01:03 PM
Hi,
I'm trying to port a PlayBook app in which I need to retrieve a list of directories. It's easy with
But unfortunately it's not as easy with the BB10 api.
function directories(dossier){
var rep= new Array;
blackberry.io.sandbox = false;
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
window.requestFileSystem(window.TEMPORARY, 1024 * 1024,
function (fs) {
fs.root.getDirectory(dossier, {create: false},
function (directoryEntry) {
var reader = directoryEntry.createReader();
reader.readEntries(
function (entries) {
for(var i = 0; i < entries.length; i++) {
var entry = entries[i];
if (entry.isDirectory){
rep[i] = entry.name;
}
}
//return true;
},errorHandler);
},errorHandler);
});
return rep;
}
the function returns always an empty array. I tried to make rep a global variable but it's the same. It looks like my code
rep[i] = entry.name;
is executed after return rep;
I'm not sure how I could make a function wich returns the list of directories contained inside a given folder.
Any help would be appreciated. Thanks & happy new year,
Laurent
01-03-2013 09:10 AM
I'm not sure if you had a chance to check out this posting http://www.html5rocks.com/en/tutorials/file/filesy
There is one thing I might highlight:
"..There is no guarantee that all of a directory's entries will be returned in a single call to readEntries(). That means you need to keep calling DirectoryReader.readEntries()until no more results are returned.."
Did you try calling readEntries multiple times, untile it returns undefined? Just a quick thought. If you still have problems with it we can try something further.
01-03-2013 09:25 AM
Thank you Erik.
Maybe I could give it a try. But, if a put alerts like this :
function directories(dossier){
var rep= new Array;
blackberry.io.sandbox = false;
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
window.requestFileSystem(window.TEMPORARY, 1024 * 1024,
function (fs) {
fs.root.getDirectory(dossier, {create: false},
function (directoryEntry) {
var reader = directoryEntry.createReader();
reader.readEntries(
function (entries) {
for(var i = 0; i < entries.length; i++) {
var entry = entries[i];
if (entry.isDirectory){
rep[i] = entry.name;
alert(rep[i]); //I get all the directory entries
}
}
//return true;
},errorHandler);
},errorHandler);
});
alert(rep[0]); //returns undefined
return rep;
}
At first alert(rep[0]); is displayed --> undefined
then
alert(rep[i]); displays the directories
What's wrong is the order because it looks like requestfilesystem is running "in an other thread". It's not finished when the function returns rep and I don't know how to block the execution before returning my empty array rep.
01-03-2013 09:41 AM - edited 01-03-2013 09:43 AM
That looks consistent with home the HTML5 API is implemented by webkit. Of course rep[0] will be undefined. What if you do the same check they have?
var entries = [];
if(!resp.length) {
// Break and do your alert
alert(entries);
} else {
entries = entries.concat(resp.sort());
}
// Thoughts?
01-04-2013 10:17 AM
Hello, I tried your code and I have the same issue.
I think it's not possible to make a function which one returns an array of what is on a directory because window.requestFileSystem takes time to run and it is not blocking the rest of the code.
I need a way to test the end of the execution of window.requestFileSystem to be sure my array is filled but how is it possible ???
01-07-2013 05:02 AM
Sorry but I can't find the way to write a javascript function that returns all the subdirectories of a given directory.
Before BB10, it's easy : String[] blackberry.io.dir.listDirectories (path : String).
But now, how is it possible ???
01-22-2013 09:28 AM
For visibility I will post this here as well....
As I posted before here is what works for me, but it depens what it is you would like to list....
1. If you want to list the "sandbox" then you will only see files that your webview has created in the current session, or where created using the persistent tag from before, in this case you must have sandboxing set to false.
2. If you wish to list what is inside your .bar file then the following works for me on a BB10 device: (In this case you MUST be unsandboxed)
function showLocalFileSystem() {
// Get access to the root directory, your unix home is inside the local:///data/ directory of the bar so get out of there.
fs.root.getDirectory(blackberry.io.home + '/../app/native/', {create: false}, function(dirEntry) {
var dirReader = dirEntry.createReader();
var entries = [];
var readEntries = function() {
dirReader.readEntries (function(results) {
if (!results.length) {
//No more results then print them
console.log(entries);
} else {
keep appending as we move down the directory structure
entries = entries.concat(results);
readEntries();
}
}, errorHandler);
};
readEntries(); // Start reading dirs.
});
}
Let me know if you have problems with this LaurentC