Wednesday, February 23, 2011

Re: File Access In a BlackBerry Widget

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.copyblackberry.io.file.deleteFileblackberry.io.file.existsblackberry.io.file.getFilePropertiesblackberry.io.file.openblackberry.io.file.readFileblackberry.io.file.renameblackberry.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.dateCreatedblackberry.io.file.dateModifiedblackberry.io.file.directoryblackberry.io.file.fileExtensionblackberry.io.file.isHiddenblackberry.io.file.isReadonlyblackberry.io.file.mimeTypeblackberry.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.createNewDirblackberry.io.dir.deleteDirectoryblackberry.io.dir.existsblackberry.io.dir.getFreeSpaceForRootblackberry.io.dir.getParentDirectoryblackberry.io.dir.getRootDirsblackberry.io.dir.listDirectoriesblackberry.io.dir.listFilesblackberry.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.


View the original article here

No comments:

Post a Comment