IT tutorials
 
Mobile
 

Developing BlackBerry Tablet Applications : File System Access (part 1)

5/31/2013 7:53:45 PM
- How To Install Windows Server 2012 On VirtualBox
- How To Bypass Torrent Connection Blocking By Your ISP
- How To Install Actual Facebook App On Kindle Fire

Just as in the desktop version of Adobe AIR, AIR on BlackBerry Tablet OS provides you access to the file system. The usage is exactly the same.

1. Folder Aliases

To access the file system, you can navigate using several folder static alias properties of the File class. Since you are attempting to read these shared files, you will need to select the access_shared permission when creating your project.

Let’s take a look at the following code. Within applicationComplete of the application, the application1_applicationCompleteHandler method is called and the static File properties are read and written to a String variable. This String variable is written to the text property of a TextArea component. Figure 1 shows the results. You will notice that many of the aliases return the same value, which is a path to the device’s external storage card.

<?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"
               applicationComplete="application1_applicationCompleteHandler(event)">
    <fx:Script>
        <![CDATA[
            import mx.events.FlexEvent;

            protected function application1_applicationCompleteHandler
            (event:FlexEvent):void
            {
                var s:String = "";
                s += "File.applicationDirectory : " + 
                File.applicationDirectory.nativePath + "\n\n";
                s += "File.applicationStorageDirectory : " + 
                File.applicationStorageDirectory.nativePath + "\n\n";
                s += "File.desktopDirectory: " + 
                File.desktopDirectory.nativePath + "\n\n";
                s += "File.documentsDirectory : " + 
                File.documentsDirectory.nativePath + "\n\n";
                s += "File.userDirectory : " + 
                File.userDirectory.nativePath + "\n\n";
                info.text = s;
            }

        ]]>
    </fx:Script>
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>

    <s:Label text="File System Paths" top="10" width="100%" textAlign="center"/>

    <s:TextArea id="info" width="100%" height="100%" top="40" editable="false"/>

</s:Application>

					  

Figure 1. File System Paths


2. Read and Write to the File System

Adobe AIR gives you the ability to read and write files to the file system. The following example will create a new file and then read it back.

Let’s review the following code. There are two TextArea and two Button components that make up this sample. The first TextArea with the ID of “contents” will hold the contents of what is to be written to the file; and the second, with the ID of “results,” will output the file contents when read back. The application is shown in Figure 2.

Clicking on the Button component with the label of “Save” will call the button1_clickHandler method. Within the button1_clickHandler method, an instance of File is created with the name “file,” the path is resolved to the userDirectory, and “samples/test.txt” is passed in to the resolvePath method. An instance of FileStream with the name “stream” is created to write the data to the file. The open method is called on the stream object and the file and FileMode.WRITE is passed in, which will open the file with write permissions. Next, the writeUTFBytes method is called and the contents.text is passed in. Finally, the stream is closed.

Clicking on the Button with the label of “Load” will call the button2_clickHandler method. Within the button2_clickHandler method, an instance of File is created with the name “file,” the path is resolved to the userDirectory, and “samples/test.txt” is passed in to the resolvePath method. An instance of FileStream with the name “stream” is created to read the data from the file. The open method is called on the stream object and the file and FileMode.READ is passed in, which will open the file with write permissions. Next, the readUTFBytes method is called, the stream.bytesAvailable is passed in, and the results are set to the results.text property of the second TextArea. Finally, the stream is closed. Figure 3 shows the contents of the file and the path to the newly created file within the TextArea with the ID of results.

<?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 = File.applicationStorageDirectory.
resolvePath("samples/test.txt");
                var stream:FileStream = new FileStream()
                stream.open(file, FileMode.WRITE);
                stream.writeUTFBytes(contents.text);
                stream.close();
            }

            protected function button2_clickHandler(event:MouseEvent):void
            {
                var file:File = File. applicationStorageDirectory.
resolvePath("samples/test.txt");
                var stream:FileStream = new FileStream()
                stream.open(file, FileMode.READ);
                results.text = stream.readUTFBytes(stream.bytesAvailable);
                stream.close();
            }

        ]]>
    </fx:Script>

    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>

    <s:TextArea id="contents" left="10" right="10" top="10" height="100"/>
    <s:Button right="10" top="120" label="Save" click="button1_clickHandler(event)"/>
    <s:Label id="path" left="10" top="160"/>
    <s:Button left="10" top="200" label="Load" click="button2_clickHandler(event)"/>
    <s:TextArea id="results" left="10" right="10" top="280" height="100" 
editable="false"/>
</s:Application>

					  

Figure 2. File Save Application


Figure 3. File contents loaded into results TextArea and file path displayed


 
Others
 
- iPhone Developer : Assembling Views and Animations - Managing Subviews - Tagging and Retrieving Views
- iPhone Developer : Assembling Views and Animations - Recovering a View Hierarchy Tree, Querying Subviews
- Java ME on Symbian OS : Handling Diversity - Handling Screen and Display Diversity
- Java ME on Symbian OS : Handling Diversity - Handling Diverse Multimedia Formats and Protocols
- Java ME on Symbian OS : Handling Diversity - Supporting Diverse Input Mechanisms
- Windows Phone 7 : Building 2D Games with the XNA Framework - AlienShooter Game Play (part 6) - Updated GameplayScreen Class, Collision Detection and Memory Management
- Windows Phone 7 : Building 2D Games with the XNA Framework - AlienShooter Game Play (part 5) - Missile Class, Game Status Board Class
- Windows Phone 7 : Building 2D Games with the XNA Framework - AlienShooter Game Play (part 4) - Hero Ship Class
- Windows Phone 7 : Building 2D Games with the XNA Framework - AlienShooter Game Play (part 3) - Enemy Class
- Windows Phone 7 : Building 2D Games with the XNA Framework - AlienShooter Game Play (part 2) - Game Object Class
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
programming4us programming4us
 
Popular tags
 
Video Tutorail Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Indesign Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe After Effects Adobe Photoshop Adobe Fireworks Adobe Flash Catalyst Corel Painter X CorelDRAW X5 CorelDraw 10 QuarkXPress 8 windows Phone 7 windows Phone 8 BlackBerry Android Ipad Iphone iOS