[AIR] 对存储器(Storage Volume)监听

AIR 2.0及以上提供了对系统的存储器信息访问和监听的API。通过这些API,你不仅可以访问到硬盘文件系统,同时还可以监听通过USB或火线进行连接的移动存储设备,例如移动硬盘和以存储方式连接到计算机的相机,MP3或手机等等。
你可以通过对系统存储器信息添加监听器来监听这些外部设备的连接、断开状态。

代码示例:

import flash.filesystem.StorageVolumeInfo;

import flash.events.StorageVolumeChangeEvent;

import flash.filesystem.StorageVolume;

import flash.filesystem.File;

var volumes:Vector.<StorageVolume >  = new Vector.<StorageVolume >   ;

volumes = StorageVolumeInfo.storageVolumeInfo.getStorageVolumes();

for (var i:int = 0; i < volumes.length; i++)

{

    trace(volumes[i].rootDirectory.nativePath);//每个装载的存储卷的根目录的本机路径

    trace(volumes[i].name);//存储卷名称,命名即为null

    trace(volumes[i].rootDirectory.getDirectoryListing());//存储卷根目录下文件列表

    trace(volumes[i].drive);//存储卷盘符

    //遍历所有存储卷根目录下是否存在DIGITALBOARD文件夹

    for (var j:int = 0; j<volumes[i].rootDirectory.getDirectoryListing().length; j++)

    {

        //trace(volumes[i].rootDirectory.getDirectoryListing()[j].name);

        if (volumes[i].rootDirectory.getDirectoryListing()[j].name == "DIGITALBOARD")

        {

            trace(volumes[i].rootDirectory.nativePath);

            trace(File.desktopDirectory);

            //var file:File = volumes[i].rootDirectory.nativePath

            var file:File = File(volumes[i].rootDirectory.resolvePath(volumes[i].rootDirectory.nativePath + "DIGITALBOARD"));//+File.separator

            trace(volumes[i].rootDirectory.nativePath+"DIGITALBOARD");

            //file.copyTo(File.desktopDirectory);

            //file.browseForOpen("打开");

            //file.addEventListener(Event.SELECT,onSelect);

            return;

        }

    }

}

function onSelect(e:Event):void

{

    trace("select",e.target.name);

}



//监听外部设备连接和断开状态

if (StorageVolumeInfo.isSupported)

{

    trace("平台支持 StorageVolumeInfo 类");

    //卷装入;

    StorageVolumeInfo.storageVolumeInfo.addEventListener(StorageVolumeChangeEvent.STORAGE_VOLUME_MOUNT, onVolumeMount);

    //存储卷卸载;

    StorageVolumeInfo.storageVolumeInfo.addEventListener(StorageVolumeChangeEvent.STORAGE_VOLUME_UNMOUNT, onVolumeUnmount);

}

else

{

    trace("平台不支持 StorageVolumeInfo 类");

}



function onVolumeMount(e:StorageVolumeChangeEvent):void

{

    trace( e.storageVolume.name,"检测到可移动磁盘" );

    //trace( e.storageVolume.fileSystemType );

    //trace( e.storageVolume.isRemovable );

    //trace( e.storageVolume.isWritable );

    //trace( e.storageVolume.rootDirectory );

    //trace( e.storageVolume.rootDirectory.nativePath );

}

function onVolumeUnmount(e:StorageVolumeChangeEvent):void

{

    trace( e.storageVolume.name,"卸载可移动磁盘" );

    //trace(e.storageVolume.drive);

}

 

你可能感兴趣的:(AIR)