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