3. File Browse for Single File
The browse for file functionality of the File
class works a bit differently in BlackBerry Tablet OS as compared to the
desktop version. Within BlackBerry Tablet OS, the
browseForOpen method will open up a specific native
file selector that will allow you to open a file of type Audio, Image,
Documents, or Video. Since you are attempting to read these shared
files, you will need to select the access_shared
permission when creating your project.
Let’s review the next code sample. The button with the “Browse”
label will call the button1_clickHandler when
clicked. Within this function, an instance of File is
created with the variable name “file.” An event listener listening for
the Event.SELECT event is added to the
File object, then the
browseForOpen method is called. The application is
shown in Figure 4. When
browseForOpen is called, the BlackBerry Tablet OS
file selector is launched (see Figure 5). After
selecting a file within the BlackBerry Tablet OS file selector, the event is fired and the
onFileSelect method is called. The
event.currentTarget is cast to a
File object and its nativePath,
extension, and url properties
are used to display the nativePath and the
image in the example shown in Figure 5-6.
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark">
<fx:Script>
<![CDATA[
protected function button1_clickHandler(event:MouseEvent):void
{
var file:File = new File();
file.addEventListener(Event.SELECT, onFileSelect);
file.browseForOpen("Open");
}
private function onFileSelect(event:Event):void {
var file:File = File(event.currentTarget);
filepath.text = file.nativePath;
if(file.extension == "jpg"){
image.source = file.url;
}
}
]]>
</fx:Script>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:Button horizontalCenter="0" top="10" label="Browse"
click="button1_clickHandler(event)"/>
<s:Label id="filepath" left="10" right="10" top="100"/>
<s:Image id="image" width="230" height="350" top="150" horizontalCenter="0"/>
</s:Application>