Using the File API to explore the Tablet OS's filesystem

by Retired on ‎08-30-2011 05:03 PM (4,749 Views)

This article with will demonstrate the following feature of the File API

 

  • Create a reference to the shared folder
  • Retrieve the files/folders stored within a referenced folder
  • Open files using the application registered by the BlackBerry® Tablet OS for respective file
  • Delete a file from the file system

 

Create a reference to the shared folder

 

var file:File = File.userDirectory.resolvePath("shared");

File.userDirectory will map to the application's working directory. Due to security restriction, you can't create or reference folders outside the application's directory other than the shared folder. For more info in regards to file system mapping, please see BlackBerry PlayBook File System Layout

 

Retrieve the files/folders stored within a referenced folder

To get retrieve files/folders stored within a referenced folder, you can you the File.getDirectoryListing() method.

 

var fileList:ArrayCollection = file.getDirectoryListing();

fileList is an ArrayCollection object where each element within the ArrayCollection corresponds to the file/folder within the referenced folder.

 

To customize the ArrayCollection to contain custom data about the file system, you can use the File.getDirectoryListingAsync(). In the below example, we are customizing the data for a List object.

 

[Bindable]
private var file:File = File.userDirectory;
//fileList is used for the Flex List dataprovider
[Bindable]
private var fileList:ArrayCollection = new ArrayCollection();

file.addEventListener(FileListEvent.DIRECTORY_LISTING, listDirectory);
file.getDirectoryListingAsync();

protected function listDirectory(event:FileListEvent):void {
	// Retrieving the directory listing
	var fileArray:Array = event.files;
	// Starting a fresh fileList
	fileList.removeAll();

	// Formatting the directory listing to retrieving needed info 
	for (var i:int = 0; i < fileArray.length; i++) 
	{
		var nodeFile:File = fileArray[i];
		if( nodeFile.exists){
			var newData:Object = {name:nodeFile.name,
				size:nodeFile.size,
				file:nodeFile};
			fileList.addItem(newData);
		}
	}
}

 

Open files using the application registered by Tablet's OS for respective file

// directoryList.selectedItem.file is the selected file Object in the spark List
if(directoryList.selectedItem.file.isDirectory){
	file = directoryList.selectedItem.file;
}
else {
	directoryList.selectedItem.file.openWithDefaultApplication();
}

 

Delete a file from the file system

// directoryList.selectedItem.file is the selected file Object in the spark List
if(directoryList.selectedItem.file.isDirectory){
	directoryList.selectedItem.file.deleteDirectory(true);
}
else {
	directoryList.selectedItem.file.deleteFile();
}

IMG_00000005.jpg

Screenshot of the sample application which is attached

 

See the following learning resources for more information on the API used in this article:

The following attached File Manager example illustrates how to use the File API