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

  • 本站文章除注明转载外,均为本站原创或者翻译。
  • 本站文章欢迎各种形式的转载,但请18岁以上的转载者注明文章出处,尊重我的劳动,也尊重你的智商;
  • 本站部分原创和翻译文章提供markdown格式源码,欢迎使用文章源码进行转载;
  • 本博客采用 WPCMD 维护;
  • 本文标题:AIR2新功能-侦测USB存储设备
  • 本文链接:http://zengrong.net/post/866.htm

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

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

代码范例:

<?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"
                       applicationComplete="init()">
    <fx:Script>
        <![CDATA[
            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('卸载');
            }
        ]]>
    </fx:Script>
</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('卸载');
        }

“`