File Access In a BlackBerry Widget

Tips and Tricks

The BlackBerry® Widget API allows web developers to very easily access files that are stored on the BlackBerry® smartphone or on the SD Card. Web developers are able to store JavaScript, CSS or any media files locally on the BlackBerry smartphone to allow for quick access to those files. This can go a long way in optimizing your BlackBerry Widgets and also open doors for the capabilities of your applications. By leveraging the file access abilities through the

Accessing the file storage on the BlackBerry smartphone is as simple as specifying a path variable. The following example demonstrates how to open and read a file from the SD card using the BlackBerry Widget API’s:

var filePath = “file:///SDCard/myDataFile.txt”;

function openFile() {
if (blackberry.io.file.exists(filePath)) {
//file exists so load its contents
blackberry.io.file.readFile(filePath, readFile);
alert(“File successfully loaded”);
} else {
//file does not exist so display that to the user
alert(“File does not exist”);
}
}
function readFile(filePath, data) {
//retrieves the text stored in the file
var temp = blackberry.utils.blobToString(data);
}

Using these API’s, you can access every facet of the file. The API’s that are provided out of the box are:

  • blackberry.io.file.copy
  • blackberry.io.file.deleteFile
  • blackberry.io.file.exists
  • blackberry.io.file.getFileProperties
  • blackberry.io.file.open
  • blackberry.io.file.readFile
  • blackberry.io.file.rename
  • blackberry.io.file.saveFile

In addition to having full access to the file API’s, as a developer you’re also given access to all of the members of the File object to interact with the file:

  • blackberry.io.file.dateCreated
  • blackberry.io.file.dateModified
  • blackberry.io.file.directory
  • blackberry.io.file.fileExtension
  • blackberry.io.file.isHidden
  • blackberry.io.file.isReadonly
  • blackberry.io.file.mimeType
  • blackberry.io.file.size

And what would having file access be without having access to creating and managing directories on the file system. The BlackBerry Widget SDK has also provided functionality to use directory structures, so you’ll be able to keep your application clean and leave a minimal footprint on the file system:

  • blackberry.io.dir.createNewDir
  • blackberry.io.dir.deleteDirectory
  • blackberry.io.dir.exists
  • blackberry.io.dir.getFreeSpaceForRoot
  • blackberry.io.dir.getParentDirectory
  • blackberry.io.dir.getRootDirs
  • blackberry.io.dir.listDirectories
  • blackberry.io.dir.listFiles
  • blackberry.io.dir.rename

With all of the control that the BlackBerry Widget API’s give over the file structure and the ability to store media files directly to the BlackBerry smartphone, I hope to see all of you leverage this into your applications to make full features BlackBerry Widgets with compelling UI’s utilizing quick media access! Please feel free to leave a comment below and let us know your thoughts or requests for future BlackBerry Widget API’s.

About Prosanta B.

Prosanta is a member of the BlackBerry Developer Relations team specializing in Web Development. Prosanta’s focus is on developing out the web platform and tools associated with web development while supporting the development efforts of a number of Independent Software Vendors. Prior to joining RIM, Prosanta had worked on numerous web portals for major multinational firms writing both front and backend code.

Join the conversation

Show comments Hide comments
+ -
blog comments powered by Disqus