在工作中遇到通过Environment.getExternalStorageDirectory().getAbsolutePath()获取不到外插sdcard路径。看了intel写的一个文件管理器发现他是这样获取的。
StorageManager mStorageManager = storageManager;
if(mStorageManager == null){
Log.d(TAG , "getVolumePath , mStorageManager="+mStorageManager);
return null;
}
String volumePath = null;
final List<VolumeInfo> volumes = mStorageManager.getVolumes();//得到所有挂载
try {
List<String> publicList = new<String> ArrayList();
for (VolumeInfo vol : volumes) {
String path = vol.getPath()==null ? null :vol.getPath().toString();
boolean isUSB = vol.getDisk()!=null?vol.getDisk().isUsb():false;
boolean isSDCard = vol.getDisk()!=null?vol.getDisk().isSd():false;
boolean isPublic = vol.getType() == VolumeInfo.TYPE_PUBLIC;
Log.d(TAG , "getVolumePath , isUSB="+isUSB+",isSDCard="+isSDCard+",isPublic="+isPublic+",path="+path+",filePath="+filePath);
if(isPublic && path != null ) {
if(isSDCard || isUSB) {//判断USB还是SDCard
volumePath = path;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return volumePath;