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
Contributor
fmbois
Posts: 16
Registered: ‎08-27-2011
My Device: Curve9300, PlayBook
Accepted Solution

Read private files with HTML5 DirectoryEntry then FileEntry

[ Edited ]

Test setup: dev alpha phone load 10.9.9.339 , webworks BB10-WebWorks-SDK_1.0.4.7.exe.

 

I want to copy a local mp3 into the shared ringtones directory.

The local file (Arpege.MP3) is compiled into the app bar, it is located in my sandbox root dir, it is well visible to invoke framework, but not visible ot File IO Framework.


I can listen to it at the location "local:///Arpege.MP3" as below:
blackberry.invoke.invoke({
    type: "audio/mpeg3",
    uri: "local:///Arpege.MP3"
}, onInvokeSuccess, onInvokeError);

 

But I cannot reach it with HTML5 file system as below. The purpose is to open it as a FileEntry in order to copy it.

window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
window.requestFileSystem(window.TEMPORARY, 1024 * 1024,
  function (fs) {
    fs.root.getFile('local:///Arpege.MP3', {create: false}, function(fileEntry) {
    }, handle_error);
  },
handle_error);
It ends up with NOT_FOUND_ERR.

 

Changing 'local:///Arpege.MP3' to 'Arpege.MP3' does not work either. Creating a sub directory at root of my sandbox and placing the private file into it do not work either. Nothing in my sandbox is visible to File IO framework, be it file or directory.

By using sample code, I tried to list all directories and files withing my sandbox, I only see a chrome subdirectory and a webviews subdirectory, I don't see my index.html or icon.png either.

 

How can I work around this restriction? I only want to pack a ringtone with my app, then copy it into the shared ringtones or music directory, a real simple app (I think simpler than this one would get rejected by AppWorld).

 

The private file has been well packed into the bar when opened with winzip:

Arpege.MP3 MP3 format 14/01/2013 00.30 137090  3% 132832 native\

 

Thanks

Retired
erikjohnzon
Posts: 245
Registered: ‎09-21-2012
My Device: Q10

Re: Read private files with HTML5 DirectoryEntry then FileEntry

A couple things:

 

1. Where is this shared ringtone directory? Currently you can copy it into music, video, downloads, documents etc.

2. To access these shared directories you will need to un-sandbox your file system ie: blackberry.io.sandbox = false;

3. The shared folder is located at: blackberry.io.sharedFolder


4. When listing the directories using the HMTL5 file api behaviour can be tricky, you may need to put the call in a loop, as per the drections here, since it does not return all listings everytime. You have to continue till it is nothing: http://www.html5rocks.com/en/tutorials/file/filesystem/#toc-dir-reading

@erikjohnzon
erjohnson@blackberry.com
Contributor
fmbois
Posts: 16
Registered: ‎08-27-2011
My Device: Curve9300, PlayBook

Re: Read private files with HTML5 DirectoryEntry then FileEntry

Thanks erikjohnzon for having looked at my question. 

The funny thing is that I have no problem to access the sharedFolder, or list files; I cannot access files in my own sandbox.

 

1. You are right, on Dev Alpha there is no shared ringtones directory. I hoped to create one, and then be picked up by phone OS when user choose a ringtone from settings.

2. I have done it (blackberry.io.sandbox = false:smileywink:

3. Yes, I will write my ringtones there, probably into 'music' sub dir in blackberry.io.sharedFolder if I cannot create 'ringtones'

4. I did that too based on example code:

dirReader.readEntries (function(results) { //results type: Entry
    if (!results.length) {
        listResults(entries.sort());
    } else {
        entries = entries.concat(Array.prototype.slice.call(results || [], 0));
        readEntries();
    }
}, install_error);

 

Retired
erikjohnzon
Posts: 245
Registered: ‎09-21-2012
My Device: Q10

Re: Read private files with HTML5 DirectoryEntry then FileEntry

If you turn OFF un-sandboxed, does it change your root reference and allow you to actually access the files? What about using the full path of the file instead of local:///?

@erikjohnzon
erjohnson@blackberry.com
Contributor
fmbois
Posts: 16
Registered: ‎08-27-2011
My Device: Curve9300, PlayBook

Re: Read private files with HTML5 DirectoryEntry then FileEntry

Same NOT_FOUND_ERR with sandbox turned on:

    blackberry.io.sandbox = true;

Using the path  "local:///Arpege.MP3".

 

The path I use must already be the full path  "local:///Arpege.MP3" because it is placed just under the root of my sandbox (packed at the same level as my config.xml, index.html..), and I use that path to listen the mp3 file:

 

blackberry.invoke.invoke({
type: "audio/mpeg3",
uri: "local:///Arpege.MP3"
}, onInvokeSuccess, onInvokeError);

 

If I go into the native extention for webworks, in some client/index.js (I don't remember), the path "local///Arpege.MP3" would be translated into "file///native/Arpege.MP3", which make sense to me. From what I have seen up to now, it is not a issue of path, there two software module thrown together, they are sandboxed one from the other, the one is HTML5 (invoke ..), the other is HTML5 IO. The later is not permitted to see inside the sandbox. 

 

How do I know that HTML IO (File) is not allowed to see inside the sandbox? I used the example code to list every file inside the sandbox, every file in the sharedFolder, and every directory under the phone (looks like unix root), and default current directory. I have not found anything seeming to contain my private files. Most directory I found are well known system directories and files (such usr, bin, config, webworks..)

Contributor
fmbois
Posts: 16
Registered: ‎08-27-2011
My Device: Curve9300, PlayBook

Re: Read private files with HTML5 DirectoryEntry then FileEntry

I see what you mean by absolute path. Actually I have already tried :

    blackberry.io.home/Arpege.MP3

    blackberry.io.home/../Arpege.MP3

and up to the system root. None worked out.

Retired
erikjohnzon
Posts: 245
Registered: ‎09-21-2012
My Device: Q10

Re: Read private files with HTML5 DirectoryEntry then FileEntry

Let me see if I can figure somthing out on my end...I'll keep you posted.

@erikjohnzon
erjohnson@blackberry.com
Contributor
fmbois
Posts: 16
Registered: ‎08-27-2011
My Device: Curve9300, PlayBook

Re: Read private files with HTML5 DirectoryEntry then FileEntry

[ Edited ]

webkitResolveLocalFileSystemURI() should do the job, but it does not, at least for the moment

(  https://developer.blackberry.com/html5/apis/localfilesystem.html#webkitResolveLocalFileSystemURI    )

 

Based on the example, I tried:

 

window.webkitResolveLocalFileSystemURI("file:///Arpege.MP3",
    function(fileEntry) { //onSuccess
        console.log("sucess "+fileEntry.name);
    },
    function(e) { //onError
        console.log("error "+e.toString());
    }
);

 

But got error showing that the function is not yet implemented:

'undefined' is not a function (evaluating 'window.webkitResolveLocalFileSystemURI')

 

Others signs that this function is not finished :

1) in the example, 'file:///' is used instead of 'local:///' which is a working naming convention for Invoke framework.

2) the document states a extra 'size' argument that the example is not using.

Retired
erikjohnzon
Posts: 245
Registered: ‎09-21-2012
My Device: Q10

Re: Read private files with HTML5 DirectoryEntry then FileEntry

Okay there is a bit of confusion surrounding the API, I'll admit I was a tad confused till I tried out everything myself. In the act of transparency for future people that find this thread:

 

1. To access content from within your .bar file you will need to use blackberry.io.sandbox = false; The .bar is actually outside of your local sandbox that the "Chrome" gives you. It is a bit tricky since the browser expects you to run content as a page, with perhaps local storage and it manages this. Your .bar file is not included in this calculation.

 

2. To access files within the bar you will need to reference the absolute path to them, with the permissions being restricted. So you can write to the /data directory the OS creates for you and this is actually returned by the balckberry.io.home call in your code.

 

3. If you are NOT un-sandboxed then you will be within a special sandbox created by the browser and this is != to your bar file.

 

So to solve your original question you can create and list the contents of a directory within your  blackberry.io.home directory by issueing the following code:

 

 


blackberry.io.sandbox = false;

fs.root.getDirectory(blackberry.io.home, {create: false}, function(dirEntry) {
        var dirReader = dirEntry.createReader();
        var entries = [];
        var readEntries = function() {
             dirReader.readEntries (function(results) {
              if (!results.length) {
                console.log(entries);
              } else {
                entries = entries.concat(results);
                readEntries();
              }
            }, errorHandler);
        };

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

This gets access to the "home" directory of your application NOT your .bar root. You are encouraged to use this or the sandbox provided in the browser to store files. You can create directories as follows:
fs.root.getDirectory(blackberry.io.home + '/music', {create: true}, function(dirEntry) {
            console.log("Successfully created directory:" + dirEntry);
}, errorHandler);

I'm fairly confident you cannot write to the root of your bar explicitly, it will return a permission error.
@erikjohnzon
erjohnson@blackberry.com
Developer
laurentC
Posts: 268
Registered: ‎02-05-2010
My Device: Blackberry Z10

Re: Read private files with HTML5 DirectoryEntry then FileEntry

Eric,

Reading the directory entries is ok but all the samples are just displaying them with console.log...
What is wrong with the new BB10 file api is you can't interact with the directory entries you read, like assigning a global javascript variable.

As said in my post
http://supportforums.blackberry.com/t5/Web-and-WebWorks-Development/BB10-directories-list/m-p/208023...
you can't make a function that returns an array of a given directory.

I know the aim is to respect the HTML5 standard but the "old" api was so convenient...

I think I'll redevelop my app with Cascades...

Laurent

Twitter : @LaurentKP
My apps