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

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

代码范例:

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
<?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('卸载');
}

```

留言

2009-12-07
次访问