Not yet rated
Tags:



Problem

Ability to access and detect mass storage changes

Solution

The following application will display the existing storage devices available as well as add and remove mass storage devices in case you add a new device or remove a device such as a USB key.

Detailed explanation

A missing element from previous versions of AIR was the ability to access and detect mass storage changes.  In AIR 2.0 you can detect and access the device's mass storage.  There are two classes that enable you to do so:

  • flash.filesystem.StorageVolumeInfo - The StorageVolumeInfo is a singleton manager that keeps track of changes to the mass storage devices.  Upon changes StorageVolumeChangeEvent gets dispatched. There are two types of events: storageVolumeMount and storageVolumeUnmount.
  • flash.filesystem.StorageVolume - The StorageVolume class holds information regarding the properties of the mass storage volume.

Using the new API, it is possible to query the name, label, root directory, and file system type of a volume.  Additionally, it is possible to determine whether a volume is writable or removable.   The following application will display the existing storage devices available as well as add and remove mass storage devices in case you add a new device or remove a device such as a USB key.

<?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/mx"
                           
creationComplete="creationCompleteHandler()">
    
     <fx:Script>
          <![CDATA[
               import flash.events.StorageVolumeChangeEvent;
               import mx.collections.ArrayCollection;
              
               [Bindable]
               private var storageCollection:ArrayCollection = new
ArrayCollection();

               protected function creationCompleteHandler():void
               {
                    var storageVolumes:Vector.<StorageVolume>
= StorageVolumeInfo.storageVolumeInfo.getStorageVolumes();
                    var length:int = storageVolumes.length;
                   
                    addEventListeners();
                   
                    for (var i:int = 0; i < length; i++)
                    {
                         addItem( storageVolumes[i] );
                    }
               }
              
               private function addEventListeners():void
               {
                   
StorageVolumeInfo.storageVolumeInfo.addEventListener(StorageVolumeChangeEvent.STORAGE_VOLUME_MOUNT,
                    function (event:StorageVolumeChangeEvent):void
                    {
                         addItem(event.storageVolume);
                    });
                   
                   
StorageVolumeInfo.storageVolumeInfo.addEventListener(StorageVolumeChangeEvent.STORAGE_VOLUME_UNMOUNT,
                    function (event:StorageVolumeChangeEvent):void
                    {
                         var nativePath:String =
event.rootDirectory.nativePath;
                         removeItemByNativePath( nativePath );
                    });
               }
              
               private function addItem(
storageVolume:StorageVolume ):void
               {
                    var object:Object = new Object();
                   
                    object = new Object();
                    object.name = storageVolume.name;
                    object.icon =
storageVolume.rootDirectory.icon.bitmaps[2];
                    object.nativePath =
storageVolume.rootDirectory.nativePath;
                    object.isWritable = storageVolume.isWritable;
                    object.isRemovable = storageVolume.isRemovable;
                    storageCollection.addItem( object );           
       
               }
              
               private function removeItemByNativePath(
nativePath:String ):void
               {
                    var len:Number = this.storageCollection.length;
                    var object:Object;
                   
                    for ( var i:int=0; i<len; i++ )
                    {
                         object = this.storageCollection.getItemAt(
i );
                        
                         if ( object.nativePath == nativePath )
                         {
                              this.storageCollection.removeItemAt(
i );
                              break;
                         }
                    }
               }

          ]]>
     </fx:Script>
    
     <s:List x="87" y="28"
               skinClass="components.DataList"
               dataProvider="{storageCollection}"/>
    
</s:WindowedApplication>

 

Ability to access and detect mass storage changes

To learn more about this feature and more order AdvancED Flex 4


+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes