Android M上,每次挂载OTG的时候,显示的设备是不一样的.这样子无法去判断挂载的设备的路径和区分sdcard与OTG(U盘).
网上查找了大量的文档,查看android源码,找到一个思路.做法是根据partition.diskId来区分的.
直接上代码:
public void getDiskInfo() { StorageManager mstorageManager = (StorageManager)this.getApplicationContext().getSystemService(Context.STORAGE_SERVICE); try { Method methodGetDisks = StorageManager.class.getMethod("getDisks"); Method methodGetStorageVolumes = StorageManager.class.getMethod("getVolumeList"); Method getVolumeById = StorageManager.class.getMethod("findVolumeById", String.class); StorageVolume[] storageVolumes = (StorageVolume[]) methodGetStorageVolumes.invoke(mstorageManager); List disks = (List) methodGetDisks.invoke(mstorageManager); //DiskInfo Class<?> diskIndoClass = Class.forName("android.os.storage.DiskInfo"); Method mGetDiskId = diskIndoClass.getMethod("getId"); Field diskName = diskIndoClass.getField("label"); //StorageVolume Class<?> storageVolumeClass = Class.forName("android.os.storage.StorageVolume"); Method mGetStorageVolId = storageVolumeClass.getMethod("getId"); Method mGetStorageVolDescription = storageVolumeClass.getMethod("getDescription", Context.class); Method mGetStorageVolPath = storageVolumeClass.getMethod("getPath"); Method isRemovable = storageVolumeClass.getMethod("isRemovable"); Method getVolumeState = StorageManager.class.getMethod("getVolumeState", String.class); //VolumeInfo Class<?> volumeClass = Class.forName("android.os.storage.VolumeInfo"); Method volumeDiskId = volumeClass.getMethod("getDiskId"); for(int i = 0; i < disks.size(); i++) { DiskInfo diskInfo = new DiskInfo(); Parcelable parcelable = (Parcelable) disks.get(i); diskInfo.diskId = (String) mGetDiskId.invoke(parcelable); Log.e("test", "diskid : " + diskInfo.diskId); String des = (String) diskName.get(parcelable); Log.e("test", "diskName : " + des); diskInfo.name = des; mDiskList.add(diskInfo); } for(int j = 0; j < storageVolumes.length; j++) { DiskPartition partition = new DiskPartition(); StorageVolume storageVolume = storageVolumes[j]; partition.partitionId = (String) mGetStorageVolId.invoke(storageVolume); if("emulated".equals(partition.partitionId)) { continue; } partition.name = (String) mGetStorageVolDescription.invoke(storageVolume, this); partition.path = (String) mGetStorageVolPath.invoke(storageVolume); Boolean removeAble = ((Boolean) isRemovable.invoke(storageVolume)).booleanValue(); String state = (String) getVolumeState.invoke(mstorageManager, partition.path); if("mounted".equals(state) && removeAble) { partition.diskId = (String) volumeDiskId.invoke(getVolumeById.invoke(mstorageManager, partition.partitionId)); for(DiskInfo diskInfo : mDiskList) { if(diskInfo.diskId.equals(partition.diskId)) { getStorageBlockInfo(partition); diskInfo.diskPartitions.add(partition); Log.e("test", "partition.name : " + partition.name); Log.e("test", "partition.diskId : " + partition.diskId); Log.e("test", "partition.path : " + partition.path); String partitionId = partition.diskId; String[] idSplit = partitionId.split(":"); if (idSplit != null && idSplit.length == 2) { if (idSplit[1].startsWith("8,")) { usbpath = partition.path; }else{ sdcard = partition.path; } } } } } } } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (NoSuchFieldException e) { e.printStackTrace(); } } demo : https://download.csdn.net/download/wende32/10432599