4. File Browse for Multiple Files
The browse for file functionality of the File
class works a bit differently in BlackBerry Tablet OS compared to the
desktop version. Within BlackBerry Tablet OS, the browseForOpenMultiple
method will open up a specific native file selector that will
allow you to open multiple files 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 following code. 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_MULTIPLE event is added to the
File object, then the
browseForOpen method is called. When
browseForOpen is called, the BlackBerry Tablet OS
file selector is launched (see Figure 7). After selecting the files
within the BlackBerry Tablet OS file selector, the event is fired and
the onMultipleFileSelect method is called. Within
this method, the array of files included in the event is looped over,
and if the file type is an image, it is added as a new element. The
results are shown in Figure 8.
<?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[
import spark.components.Image;
protected function button1_clickHandler(event:MouseEvent):void
{
var file:File = new File();
file.addEventListener(FileListEvent.SELECT_MULTIPLE,
onMultipleFileSelect);
file.browseForOpenMultiple("Open");
}
private function onMultipleFileSelect(event:FileListEvent):void {
holder.removeAllElements();
for (var i:int=0; i<event.files.length; i++){
var f:File = event.files[i] as File;
if(f.extension == "jpg"){
var image:Image = new Image();
image.source = f.url;
image.scaleX = .1;
image.scaleY = .1;
holder.addElement(image);
}
}
}
]]>
</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:Scroller top="150" horizontalCenter="0" bottom="0">
<s:VGroup id="holder"/>
</s:Scroller>
</s:Application>