Flex Blog

Search:
  • Home
  • Examples
    • Thumb

      Flex Examples

      Check out our Flex Examples!

    • Thumb

      Flash Builder Examples

      Check out our Flash Builder Examples!

    • Thumb

      AIR Examples

      Check out our AIR Examples!

    • Thumb

      Flex Mobile Examples

      Check out our Flex Mobile Examples!

    Adobe® Flex, Adobe® Flash Builder and Adobe® AIR are registered trademarks of Adobe Systems.
  • Components
    • Thumb

      WP Flex Contact Form

      Check out our WP Flex Contact Form!

    • Thumb

      Flash CountDown Plugin

      Check out our Flash CountDown Plugin!

    This is an overview of all our Flash/Flex based Components.
  • Jobs
  • Flex Books
  • Forum
  • Contact Us
Subscribe to Flex BlogSubscribe
  • Examples
  • iOS
Browse > Home / Examples / List Directory with AIR in Flex 4

List Directory with AIR in Flex 4

02 February 2010

Line Break

Author: Arjan (47 Articles) - Author Website

Arjan is a SAP Consultant specialized in ABAP and Front End development techniques like Web Dynpro, Adobe Interactive Forms, Flex and AIR. In his free time he likes to create examples for Flex-Blog and other applications using Flex, AIR and PHP. Other hobbies are movies and music. He is also the co-owner of Flex-Blog.com.


As you probably know already, with AIR you have access to the local file system. The following example shows you how you can list the contents of a directory with AIR in Flex 4. Besides showing filenames, I also want to display the icon associated with the file.


In AIR you have access to some default directories, these are the following:

  • The Desktop Directory
  • Documents Directory
  • User Directory
  • Application Directory
  • Application Storage Directory


    We’re going to build a small application that can show the contents of the directories listed above. To do this, lets create a simple UI to do this:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    <s:VGroup width="100%" height="100%" paddingBottom="5" paddingLeft="5" paddingRight="5" paddingTop="5">
        <s:HGroup width="100%">
            <s:RadioButton label="Desktop" group="{directory}" value="desktop" selected="true"/>
            <s:RadioButton label="Documents" group="{directory}" value="documents"/>
            <s:RadioButton label="User" group="{directory}" value="user"/>
            <s:RadioButton label="Application" group="{directory}" value="application"/>
            <s:RadioButton label="Application Storage" group="{directory}" value="applicationStorage"/>
        </s:HGroup>
        <mx:DataGrid width="100%" height="100%" dataProvider="{files}">
           
            <mx:columns>
                <mx:DataGridColumn dataField="icon" headerText="Filetype" width="60" itemRenderer="ImageRenderer"/>
                <mx:DataGridColumn dataField="name" headerText="Filename"/>
            </mx:columns>
           
        </mx:DataGrid>
    </s:VGroup>

    For the RadioButtons we need to declare the RadioButtonGroup in the Declarations tag:

    1
    2
    3
    <fx:Declarations>
        <s:RadioButtonGroup id="directory" change="handleDirectoryChange()"/>
    </fx:Declarations>

    The DataProvider for the DataGrid (in yout script section):

    1
    2
    [Bindable]
    private var files:ArrayCollection;

    And the ImageRenderer to show the icon of the File in the DataGrid:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    package
    {
        import flash.display.Bitmap;   
        import mx.containers.HBox;
        import mx.controls.Image;
       
        public class ImageRenderer extends HBox
        {
            private var img:Image = new Image();
            private var myBitmap:Bitmap;
           
            public function ImageRenderer(){
                // Set some layout properties
                this.setStyle("verticalAlign", "middle");
                this.setStyle("horizontalAlign", "center");
            }
           
            override public function set data(value:Object):void
            {
                super.data = value;
                // Create new Bitmap with the BitmapData from File.icon.bitmaps array
                // The first item in the Array is the biggest icon available
                myBitmap = new Bitmap( data.icon.bitmaps[0] );
                // Set the image source to the new Bitmap
                img.source = myBitmap;
                // Add the Image to the HBox
                addChild(img);
            }
        }
    }

    Now we need to create some Actionscript functions to actually list the selected directory contents. We need a function that lists the default directory and a change handler for the RadioButtonGroup to do this. Don’t forget to call the init() on the initialize event of the application!

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    private function init():void
    {
        // Default on start: resolve desktop directory
        var desktop:File = File.desktopDirectory;
        // Get desktop directory listing
        files = new ArrayCollection(desktop.getDirectoryListing());
    }

    private function handleDirectoryChange():void
    {

        var selectedDirectory:File;
        // Get the selected directory
        switch( directory.selectedValue )
        {
            case "desktop":
                selectedDirectory = File.desktopDirectory;
                break;
            case "documents":
                selectedDirectory = File.documentsDirectory;
                break;
            case "user":
                selectedDirectory = File.userDirectory;
                break;
            case "application":
                selectedDirectory = File.applicationDirectory;
                break;
            case "applicationStorage":
                selectedDirectory = File.applicationStorageDirectory;
                break;
        }
        // Get selected directory listing
        files = new ArrayCollection(selectedDirectory.getDirectoryListing());
    }

    That’s it, you’re already done. By selecting the RadioButtons the application will list the directory contens of the corresponding directory including the icons associated with the files in that directory.

    Download the AIR application here.

    The final application should look something like this:

    DirectoryList

    View source is enabled, but here’s the complete source code:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    <?xml version="1.0" encoding="utf-8"?>
    <s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
                           xmlns:s="library://ns.adobe.com/flex/spark"
                           xmlns:mx="library://ns.adobe.com/flex/halo" initialize="init()" viewSourceURL="srcview/index.html">
       
        <fx:Script>
            <![CDATA[
                import mx.collections.ArrayCollection;
               
                [Bindable]
                private var files:ArrayCollection;
               
                private function init():void
                {
                    // Default on start: resolve desktop directory
                    var desktop:File = File.desktopDirectory;
                    // Get desktop directory listing
                    files = new ArrayCollection(desktop.getDirectoryListing());
                }
               
                private function handleDirectoryChange():void
                {
               
                    var selectedDirectory:File;
                    // Get the selected directory
                    switch( directory.selectedValue )
                    {
                        case "desktop":
                            selectedDirectory = File.desktopDirectory;
                            break;
                        case "documents":
                            selectedDirectory = File.documentsDirectory;
                            break;
                        case "user":
                            selectedDirectory = File.userDirectory;
                            break;
                        case "application":
                            selectedDirectory = File.applicationDirectory;
                            break;
                        case "applicationStorage":
                            selectedDirectory = File.applicationStorageDirectory;
                            break;
                    }
                    // Get selected directory listing
                    files = new ArrayCollection(selectedDirectory.getDirectoryListing());
                }  
            ]]>
        </fx:Script>
       
       
        <fx:Declarations>
            <s:RadioButtonGroup id="directory" change="handleDirectoryChange()"/>
        </fx:Declarations>
       
        <s:VGroup width="100%" height="100%" paddingBottom="5" paddingLeft="5" paddingRight="5" paddingTop="5">
            <s:HGroup width="100%">
                <s:RadioButton label="Desktop" group="{directory}" value="desktop" selected="true"/>
                <s:RadioButton label="Documents" group="{directory}" value="documents"/>
                <s:RadioButton label="User" group="{directory}" value="user"/>
                <s:RadioButton label="Application" group="{directory}" value="application"/>
                <s:RadioButton label="Application Storage" group="{directory}" value="applicationStorage"/>
            </s:HGroup>
            <mx:DataGrid width="100%" height="100%" dataProvider="{files}">
               
                <mx:columns>
                    <mx:DataGridColumn dataField="icon" headerText="Filetype" width="60" itemRenderer="ImageRenderer"/>
                    <mx:DataGridColumn dataField="name" headerText="Filename"/>
                </mx:columns>
               
            </mx:DataGrid>
        </s:VGroup>
       
    </s:WindowedApplication>

    Related posts:

    1. CheckBox in List using MobileIconItemRenderer for Flex Mobile
    2. Save Data to File System with AIR in Flex 4
    3. Reading & Writing files in Adobe AIR
    4. Using the ProgressBar with a file download in Flex
    5. Data Dependent decoratorClass in MobileIconItemRenderer Example

    Written by Arjan · Filed Under Examples 

    Was this post useful to you?

    Please rate this post, follow us @ twitter, or link to this page from your website!

    1 Star2 Stars3 Stars4 Stars5 Stars (7 votes, average: 5.00 out of 5)
    Loading ... Loading ...

    8f0da694913f7fdd2206d615c342e78edelicious

    Comments

    5 Responses to “List Directory with AIR in Flex 4”

    1. innIndia on May 10th, 2010 1:09 pm

      Excellent work! Good points to get started with ItemRenderers. Helped me a lot.

    2. ginifous on December 26th, 2010 1:47 pm

      Hi, is it possible to set the icons size instead of the biggest one available?Many thanks for this tuto.

    3. vkwave on March 3rd, 2011 7:22 am

      Very helpfull to understand itemRenderer. Many Thanks for sharing

    4. Mark on March 21st, 2011 7:36 pm

      Arjan,

      thank you very much for this post. I’m very much a newbie flex developer and I have modified your example to extend List rather than HBox.

      At my equivalent of your line #18 of your ImageRenderer.as I am getting the following error:

      Type 1020: Method marked override must override another method.

      I have googled this to death and still don’t understand why I am getting this error. I tried setting all access modifiers to public to see what that would do and that didn’t help.

      Do you have any ideas as to what might be my problem?

      Again, many, many thanks for your post!

      best,

      Mark

    5. Mark on March 21st, 2011 10:53 pm

      sorry about prior post – I’ve realized some of my problems/errors

    Get Adobe Flash player

    • +1?

    • Support Flex Blog!

    • $ 13 raised
      • 2012/01/13 8:22 PM Russell Brown donated $ 3.00
      • 2011/10/31 4:43 PM Steve Dakin donated $ 5.00
      • 2011/05/11 3:37 PM Roelof Albers donated $ 5.00
    • Stay in touch!

    • Popular Tags

      • AdvancedDataGrid
      • AIR
      • ArrayCollection
      • baseColor
      • Button
      • CursorManager
      • DataGrid
      • Dynamic
      • Effects
      • File
      • FileStream
      • Flash Builder
      • Flash Builder 4
      • Flex 4
      • Flex Mobile
      • Framework
      • Icon
      • Image
      • itemRenderer
      • LinkBar
      • Mobile
      • PHP
      • ProgressBar
      • Repeater
      • Style
      • SWIZ
      • Timer
      • Tree
      • Twitter
      • ViewStack
    • Advertisements

    • Recent Posts

      • Spooky Frenzy – iPad Game
      • Fountain Example
      • Reading & Writing files in Adobe AIR
      • CheckBox in List using MobileIconItemRenderer for Flex Mobile
      • Data Dependent decoratorClass in MobileIconItemRenderer Example
      • Flex 4 Resize Effect Example
      • Jump to next field using the Focus Manager
      • Searching Data using a Class Example
      • Flex Mobile: Two finger tap gesture to toggle actionBar visibility in a View (AIR for Android)
      • TabbedMobileApplication Example in Flex Mobile (AIR for Android)
    • Categories

      • Examples
      • Guest Poster
      • iOS
    • Archives

      • September 2011
      • July 2011
      • May 2011
      • March 2011
      • February 2011
      • November 2010
      • October 2010
      • September 2010
      • August 2010
      • June 2010
      • May 2010
      • April 2010
      • March 2010
      • February 2010
      • January 2010
      • December 2009
      • November 2009
      • October 2009
      • March 2009
      • February 2009
    • Blogroll

      • Adobe Flex Jobs
      • NL for Business
    • Meta

      • Register
      • Log in
      • WordPress
      • XHTML

    Copyright © 2010 Flex Blog · AdobeĀ® and AdobeĀ® Flex are registered trademarks of Adobe Systems.

    WordPress Adobe Flex Adobe Flash Builder Adobe AIR Creative Commons License

    • Popular Posts

      • Progressbar in Datagrid Example 13 votes, average: 5.00 out of 513 votes, average: 5.00 out of 513 votes, average: 5.00 out of 513 votes, average: 5.00 out of 513 votes, average: 5.00 out of 5 (5.00 out of 5)
      • Data Dependant Tree Icon with Tree in AdvancedDataGrid with iconFunction 8 votes, average: 5.00 out of 58 votes, average: 5.00 out of 58 votes, average: 5.00 out of 58 votes, average: 5.00 out of 58 votes, average: 5.00 out of 5 (5.00 out of 5)
      • List Directory with AIR in Flex 4 7 votes, average: 5.00 out of 57 votes, average: 5.00 out of 57 votes, average: 5.00 out of 57 votes, average: 5.00 out of 57 votes, average: 5.00 out of 5 (5.00 out of 5)
      • Flex FlashVars in AS3 Example 7 votes, average: 5.00 out of 57 votes, average: 5.00 out of 57 votes, average: 5.00 out of 57 votes, average: 5.00 out of 57 votes, average: 5.00 out of 5 (5.00 out of 5)
      • Flex Dynamic Chart Example 4 votes, average: 5.00 out of 54 votes, average: 5.00 out of 54 votes, average: 5.00 out of 54 votes, average: 5.00 out of 54 votes, average: 5.00 out of 5 (5.00 out of 5)