AIR2新功能-侦测USB存储设备

通过侦测 StorageVolumeInfo 这个单例类的 StorageVolumeChangeEvent 事件来判断是否有USB存储设备插入了。需要注意以下几点:

  • 如果在设备插入之后运行的程序,那么这个设备被拔出的事件就不会被侦测到了(不知是否是beta的原因)
  • 不能使用 StorageVolumeChangeEvent 事件侦测已经装载的USB设备,但可以使用 StorageVolumeInfo.storageVolumeInfo.getStorageVolumes() 方法来获取已装载设备的 vector 数组

代码范例:

 1<?xml version="1.0" encoding="utf-8"?>
 2<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
 3                       xmlns:s="library://ns.adobe.com/flex/spark" 
 4                       xmlns:mx="library://ns.adobe.com/flex/halo"
 5                       applicationComplete="init()">
 6    <fx:Script>
 7        <![CDATA[
 8            import flash.events.StorageVolumeChangeEvent;
 9            
10            private function init():void
11            {
12                flash.filesystem.StorageVolumeInfo.storageVolumeInfo.addEventListener(StorageVolumeChangeEvent.STORAGE_VOLUME_MOUNT, onVolumeMount);
13                flash.filesystem.StorageVolumeInfo.storageVolumeInfo.addEventListener(StorageVolumeChangeEvent.STORAGE_VOLUME_UNMOUNT, onVolumeUnmount);
14                var volumes:Vector.<StorageVolume> = flash.filesystem.StorageVolumeInfo.storageVolumeInfo.getStorageVolumes();
15                trace('已有的存储设备列表:');
16                for each (var volume:StorageVolume in volumes)
17                {
18                    trace(volume.name, volume.rootDirectory.nativePath);
19                }
20            }
21            
22            private function onVolumeMount(e:StorageVolumeChangeEvent):void
23            {
24                trace('加载USB存储设备');
25                trace(e.storageVolume.drive);
26                trace(e.storageVolume.fileSystemType);
27                trace(e.storageVolume.isRemovable);
28                trace(e.storageVolume.isWritable);
29                trace(e.storageVolume.name);
30                trace(e.storageVolume.rootDirectory.url);               
31            }
32            
33            private function onVolumeUnmount(e:StorageVolumeChangeEvent):void
34            {
35                trace('卸载');
36            }
37        ]]>
38    </fx:Script>
39</s:WindowedApplication>
        import flash.events.StorageVolumeChangeEvent;
        
        private function init():void
        {
            flash.filesystem.StorageVolumeInfo.storageVolumeInfo.addEventListener(StorageVolumeChangeEvent.STORAGE_VOLUME_MOUNT, onVolumeMount);
            flash.filesystem.StorageVolumeInfo.storageVolumeInfo.addEventListener(StorageVolumeChangeEvent.STORAGE_VOLUME_UNMOUNT, onVolumeUnmount);
            var volumes:Vector.<StorageVolume> = flash.filesystem.StorageVolumeInfo.storageVolumeInfo.getStorageVolumes();
            trace('已有的存储设备列表:');
            for each (var volume:StorageVolume in volumes)
            {
                trace(volume.name, volume.rootDirectory.nativePath);
            }
        }
        
        private function onVolumeMount(e:StorageVolumeChangeEvent):void
        {
            trace('加载USB存储设备');
            trace(e.storageVolume.drive);
            trace(e.storageVolume.fileSystemType);
            trace(e.storageVolume.isRemovable);
            trace(e.storageVolume.isWritable);
            trace(e.storageVolume.name);
            trace(e.storageVolume.rootDirectory.url);               
        }
        
        private function onVolumeUnmount(e:StorageVolumeChangeEvent):void
        {
            trace('卸载');
        }