[AIR] 获取U盘,打开U盘

示例:

获取存储卷的方法:

package com.controls
{
    import flash.events.StorageVolumeChangeEvent;
    import flash.filesystem.StorageVolume;
    import flash.filesystem.StorageVolumeInfo;

    /**
     * @author Frost.Yen
     * @E-mail 871979853@qq.com
     * @create 2016-10-10 下午3:31:57
     *
     */
    public class StorageVolumeControl
    {
        
        public function StorageVolumeControl()
        {
            //监听外部设备连接和断开状态
            if (StorageVolumeInfo.isSupported)
            {
                trace("平台支持 StorageVolumeInfo 类");
                //卷装入;
                StorageVolumeInfo.storageVolumeInfo.addEventListener(StorageVolumeChangeEvent.STORAGE_VOLUME_MOUNT, onVolumeMount);
                //存储卷卸载;
                StorageVolumeInfo.storageVolumeInfo.addEventListener(StorageVolumeChangeEvent.STORAGE_VOLUME_UNMOUNT, onVolumeUnmount);
            }
            else
            {
                trace("平台不支持 StorageVolumeInfo 类");
            }
        }
        private 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 );
        }
        private function onVolumeUnmount(e:StorageVolumeChangeEvent):void
        {
            trace("卸载可移动磁盘" );
        }
        /**
         * 获取所有 存储卷对象StorageVolume
         * @return 
         * 
         */        
        public function getStorageVolumes():Vector.<StorageVolume>
        {
            var volumes:Vector.<StorageVolume >  = new Vector.<StorageVolume >;
            volumes = StorageVolumeInfo.storageVolumeInfo.getStorageVolumes();
            return volumes;
        }
        /**
         * 获取可以可删除的存储卷( 操作系统将存储卷视为可删除 (true) 还是不可删除 (false)。) 
         * @return 
         * 
         */        
        public function getRemovable():Vector.<StorageVolume>
        {
            var removable:Vector.<StorageVolume> = new Vector.<StorageVolume>();
            var volumes:Vector.<StorageVolume >= getStorageVolumes();
            for (var i:int = 0; i < volumes.length; i++)
            {
                if (volumes[i].isRemovable)
                {
                    trace(volumes[i].rootDirectory.nativePath);
                    removable.push(volumes[i]);
                }
            }
            return removable;
        }
    }
}

打开第一个U盘:

var volumes:Vector.<StorageVolume> = _storageVolume.getRemovable();
if(volumes.length){
    var file2:File =new File(volumes[0].rootDirectory.nativePath);
    file2.openWithDefaultApplication();
}

 

posted on 2016-12-19 10:44  晏过留痕  阅读(326)  评论(0编辑  收藏