Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Web and WebWorks Development

Reply
Regular Contributor
andreyavram
Posts: 76
Registered: ‎06-02-2012
My Carrier: Orange

Re: Read private files with HTML5 DirectoryEntry then FileEntry

This is the exact problem I had a while ago. I also opened a thread here http://supportforums.blackberry.com/t5/Web-and-WebWorks-Development/bb10-app-internal-directory-list... but I think no one really understood what I really wanted :smileyhappy:

If you found a solution please let us know.

I agree that the old directory api was better regarding this problem, RIM should fix this in a future version of webworks...

Please use plain text.
BlackBerry Development Advisor (Retired)
erikjohnzon
Posts: 245
Registered: ‎09-21-2012
My Carrier: Virgin

Re: Read private files with HTML5 DirectoryEntry then FileEntry

Yes perhaps the old API was more convenient, but I disagree it is not possible. In any event, if you feel the Cascades API provides an easier runtime by all means I encourage you to use it, it's a great product.

@erikjohnzon
erjohnson@blackberry.com
Please use plain text.
Developer
laurentC
Posts: 261
Registered: ‎02-05-2010
My Carrier: Bouygues Telecom

Re: Read private files with HTML5 DirectoryEntry then FileEntry

If it's possible could someone at RIM provide us a sample code ?
Twitter : @LaurentKP
My apps
Please use plain text.
BlackBerry Development Advisor (Retired)
erikjohnzon
Posts: 245
Registered: ‎09-21-2012
My Carrier: Virgin

Re: Read private files with HTML5 DirectoryEntry then FileEntry

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:

 

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
@erikjohnzon
erjohnson@blackberry.com
Please use plain text.
Developer
laurentC
Posts: 261
Registered: ‎02-05-2010
My Carrier: Bouygues Telecom

Re: Read private files with HTML5 DirectoryEntry then FileEntry

Eric,

instead of loging the directory entries into the console, make your function return an array of the files or directories inside the given directory. It's not possible. the code below is not working :

 

function showLocalFileSystem() {
    var d = new Array();
    var iter = 0;
    
    // 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);
                d[iter]= results.name;

                iter++;
                readEntries(results);
              }
            }, errorHandler);
        };
        readEntries(); // Start reading dirs.
    });
    
    return d; //returns always an empty array
}

 

Maybe there is a solution, but I can't figure out how we could achieve this with BB10 webworks.

 

Twitter : @LaurentKP
My apps
Please use plain text.
Regular Contributor
andreyavram
Posts: 76
Registered: ‎06-02-2012
My Carrier: Orange

Re: Read private files with HTML5 DirectoryEntry then FileEntry

Thanks @erikjohnzon , it's exactly what I needed!

Please use plain text.
Developer
laurentC
Posts: 261
Registered: ‎02-05-2010
My Carrier: Bouygues Telecom

Re: Read private files with HTML5 DirectoryEntry then FileEntry

@andreyavram you only need to print the entries into the console ?

Twitter : @LaurentKP
My apps
Please use plain text.
BlackBerry Development Advisor (Retired)
erikjohnzon
Posts: 245
Registered: ‎09-21-2012
My Carrier: Virgin

Re: Read private files with HTML5 DirectoryEntry then FileEntry

Of course this is an asynchronous function, if you wish to do that, then you would have to devise your own callback into the function. Just call the callback and pass the data when it is complete. I devised an example for you below to see how you can execute your code in a synchronous manner using an asynchronous function:

 

 

function showLocalFileSystem(callback) {
    var entries = [];
    fs.root.getDirectory(blackberry.io.home + '/../app/native/', {create: false}, function(dirEntry) {
        var dirReader = dirEntry.createReader();
        var readEntries = function() {
             dirReader.readEntries (function(results) {
              if (!results.length) {
                callback(entries);
              } else {
                entries = entries.concat(results);
                readEntries();
              }
            }, errorHandler);
        };

        readEntries(); // Start reading dirs.
        });
}

function showEm() {
    showLocalFileSystem(function (entries) {
        //Do whatever I was interested in doing when this funciton returns
        //Modify, etc etc
        console.log(entries);
    });
}
You need to do your work within showEm inside your callback.
@erikjohnzon
erjohnson@blackberry.com
Please use plain text.
BlackBerry Development Advisor (Retired)
erikjohnzon
Posts: 245
Registered: ‎09-21-2012
My Carrier: Virgin

Re: Read private files with HTML5 DirectoryEntry then FileEntry

If this works can we mark both the threads as solved please :smileyhappy:.

@erikjohnzon
erjohnson@blackberry.com
Please use plain text.
Regular Contributor
andreyavram
Posts: 76
Registered: ‎06-02-2012
My Carrier: Orange

Re: Read private files with HTML5 DirectoryEntry then FileEntry

[ Edited ]

No, I don't need this right now...

I will probably use this at another app...I already built the app were I initially needed this (using a workaround, not involving parsing files like this)

Please use plain text.