the Flagship of independent news, reviews and resources for Developers and Users of Samsung's mobile platform Bada
Apps & Projects, Bada Tutorials, BadaDev Projects, Fundamentals, Others

Working with Files and Directories in Bada (part 1)

wit, February 19th, 2010

Well, finally it looks like your apps won’t get all that much of freedom after all in bada when it comes to browsing and poking around in the phone’s file system. As it seems with today’s state of the SDK (1.0.0 beta 1 RC4), each app will have a sandbox directory, which can be referred to as “/Home“, where the app can store its stuff. Actually there is a limited number of directories that your application can address at all (as it has been already mentioned on the forums) :

  • /Home” – read/write – home directory of your bada app
  • /Home/Share” – read/write – directory used to share temporary data with other applications
  • /Share/[appId]” – read – directory, where you can retrieve the shared temporary data of another app by using its app ID instead of [appId]
  • /Res” – read – read-only resources directory that belongs to your app and includes diverse resources like icons
  • /Media/Images” – read – directory where you can read Images on the phone
  • /Media/Sounds” – read – directory where you can read Sounds on the phone
  • /Media/Videos” – read – directory where you can read Videos on the phone
  • /Media/Themes” – read – directory where you can read Themes on the phone
  • /Media/Others” – read – directory where you can read other media data on the phone
  • /Storagecard/Media/Images” – read – directory where you can read Images on the external memory
  • /Storagecard/Media/Sounds” – read – directory where you can read Sounds on the external memory
  • /Storagecard/Media/Videos” – read – directory where you can read Videos on the external memory
  • /Storagecard/Media/Themes” – read – directory where you can read Themes on the external memory
  • /Storagecard/Media/Others” – read – directory where you can read other media data on the external memory

These write/read permissions refer only to generic file input/output methods. Of course, your application can still write to the Media directories, but this can only be done by specific functions from the Osp::Media namespace and only if your app enjoys the corresponding privileges.

Other than that, as you can see, you can only write into your own “/Home” directory. If you were eager to share some files with another app, you could do so by creating the “/Home/Share” folder and putting those files there. On the other hand, other applications can access these files (as long as they know the right appliction’s ID) by looking into “/Share/[some_app_ID_here]“.

If you wanted to do some basic file/directory navigation and/or manipulation, the easiest way to do so on bada is by using the features provided in the Osp::Io namespace. For my Simple Finger Drawing project (as well as for other projects in the future), I have decided to create a File Dialog component, that you would use to

  • browse your Home directory as well as its child directories
  • select files to be processed (opened or saved)
  • create new files and directories

This File Dialog uses exclusively the functionality given within the Osp::Io namespace and that is what we will take a look at in this part of the article. Since this topic is large, I will separate it in multiple parts.

The FileDialog component itself is in a very young development stage, however I am sure that it already shows some decent features. Sure, it still looks a little ugly and does need some icons and stuff.

Let me show it to you. When you click on Load or Save Image the FileDialog is invoked and presents you the contents of your app’s home directory:

On this form we have only a simple List. Files have a “File: ” prefix and directories a “Dir: ” prefix. Later on I will change this to icons. The “..” is the parent directory. However since we are in “/Home”, clicking on it is senseless. The same “/Home” directory will be reloaded. Let us create a new file, click on the “New” soft button:

You can select whether you would like to create a file or a folder. In case of a file, right after clicking on “Create”, you will be asked if this is also the file you would like to select for further processing (to open it or to save data into it). Clicking on “OK” will return the full path of the file to the parent and close the File Dialog. You will receive the same message box, if you simply click on any file within the File Dialog list. Right now we click on “Cancel” and see the file appear in the list.

Now let us try to create a folder. Click again on “New”:

As you click on “Create” the File Dialog automatically changes the view and shows the contents of the new directory (empty, in most cases ;-) ). Here you could continue creating files and folders or simply change back to the parent Home directory by clicking on “Dir: ..”. There you see the new folder appear in the list. You can, of course, change back to the new directory by clicking on it.

And that is all that I have until now. I will add some more stuff to it later such as alerts (Overwrite yes/no?), deleting of files/folders, etc. Now let us take a look into some technical Osp::Io aspects of the File Dialog.

To populate the list with the contents of a directory, we will need two things:

  1. a Directory object (Directory * baseDir_;)
  2. an DirectoryEnumerator object (DirectoryEnumerator * dircontent;)

When changing into a directory (including the initial “/Home“) we must construct the Directory object first – by using the path String. It is important to check the success result here as these things may fail pretty fast if the desired directory is not available.

if(baseDir_){delete baseDir_;}
 baseDir_ = new Directory;
 r = baseDir_->Construct(base_dir);
 if(IsFailed(r))
 {
     AppLog("Failed: baseDir_->Construct(%S);", base_dir.GetPointer());
     return r;
 }
 

base_dir String holds the address of the “/Home” directory in the beginning. Once the Directory is constructed, we can read its contents using the DirectoryEnumerator.

dircontent=baseDir_->ReadN();
 if(!dircontent){return E_IO;}

 dircontent->MoveNext();          // Skip the same directory "."
 while(dircontent->MoveNext() == E_SUCCESS)
 {
     DirEntry dirEntry = dircontent->GetCurrentDirEntry();
     if(dirEntry.IsDirectory())
     {
         contentType="Dir:  ";
     } else {
         contentType="File: ";
     }
 String entry(contentType + dirEntry.GetName());
 dircontentList_->AddItem(&entry, NULL, NULL, NULL, i++);
 }

We move through the contents of the DirectoryEnumerator (as long as there is still content available). Each element is of type DirEntry (which can be both files and folders). Using its IsDirectory() function, we can treat files and folders differently. Note that the first entry of the directory is always a back reference to the same directory itself (“.”).

And this is how we get the contents of the directory into a list. In the next part I will show you how I navigate through the directories as well as how files and folders are created. Stay tuned!

Related posts:

  1. Working with Files and Directories in Bada (part 2)
  2. How To Encrypt Files In Bada
  3. Working with XML in Bada (part 1) – Simple XPath Parsing
  1. 9 Responses to “Working with Files and Directories in Bada (part 1)”

  2. By Mehdi on May 13, 2010 | Reply

    Hi,

    Thanks for this great site!

    Sadely, the perms infos are wrong on this article. You CAN write in the /Media/ folder using Osp::Media::Image

  3. By wit on May 14, 2010 | Reply

    Hi Mehdi,

    maybe this has changed in the SDK lately, I don’t know. But you surely need a privilege to write into /Media, right?

    Can’t imagine any app having write access to my Media files… :-P

  4. By Mehdi on May 14, 2010 | Reply

    Hi,

    You just need the LOCAL_CONTENT privilege AFAIK .. :)

    The method is Image::EncodeToFile(), I tested it successfully on the Simulator, but not yet on the test device.

    Mehdi

  5. By wit on May 14, 2010 | Reply

    That’s what I thought – thank you for the info, Mehdi! :-D

  6. By Avenger on May 20, 2010 | Reply

    Where is the file created ? I do not see it in the /Home directory of the simulator..
    Although the file is created without errors while debugging i cannot see the file in the folder..
    I even did a read test to make sure the file is created and some bytes are indeed added..it resulted in a success but still no file in the /Home directory..
    I used the IOExample.cpp to test this..anyone please advice.

  7. By Iosif Hamlatzis on Jul 30, 2010 | Reply

    I had the same problem couldn’t find were my image was exported.

    Finally I searched under the bada folder where the SDK is installed and found my exported images.

    They were: C:\bada\1.0.0b3\Model\Wave_LP4\Simulator\FS\Win32FS\Osp\Applications\93bt1p123e\Data

    although I would have expected them to be under /Home/

    Iosif

  1. 3 Trackback(s)

  2. Feb 22, 2010: BadaDev » Could other mobile manufacturers be interested in Bada?
  3. Feb 23, 2010: BadaDev » Working with Files and Directories in Bada (part 2)
  4. Apr 28, 2010: BadaDev » The Better Labels: EnrichedText in Bada

Post a Comment

Editor's picks

Copyright 2009-2010 BadaDev.com (unless otherwise stated). All rights reserved! Powered by Wordpress!