Android 6.0 SD卡路径获取

来源:互联网 发布:js获取所有input的值 编辑:程序博客网 时间:2024/06/14 08:00

Android6.0上会根据卡的种类和卡的挂载状态来动态生成挂载路径,因此不建议大家在按照以前的方法获取外置SD卡或者usbotg的路径,建议按照以下方法来获取:

StorageManagermStorageManager = StorageManager.from(context);finalList<VolumeInfo> vols = mStorageManager.getVolumes();VolumeInfosdcardVolume = null ; for(VolumeInfovol : vols){  if(vol.getDisk() != null && vol.getDisk().isSd())sdcardVolume = vol;  //如果需要获取usbotg的路径,则改为   // if(vol.getDisk() != null &&vol.getDisk().isUsb()) usbotgVolume = vol;} if(sdcardVolume!= null) {   String sdcardPath = sdcardVolume.getPath()==null ? null :sdcardVolume.getPath().toString();   String sdcardState = sdcardVolume.getEnvironmentForState(sdcardVolume.getState());   if(sdcardState.equals(Environment.MEDIA_MOUNTED) && sdcardPath != null){        //外置SD卡已挂载,且路径有效        // sdcardPath外置SD卡的路径   }       }


0 0
原创粉丝点击