public void getUSBHostName(Context context) {
StorageManager storageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
try {
Method getVolumeList = StorageManager.class.getDeclaredMethod("getVolumeList", null);
getVolumeList.setAccessible(true);
Class<?> classStorageVolume = Class.forName("android.os.storage.StorageVolume");
Method getPath = classStorageVolume.getDeclaredMethod("getPath", null);
Method getLabel = classStorageVolume.getDeclaredMethod("getUserLabel", null);
Object[] volumes = (Object[]) getVolumeList.invoke(storageManager, null);
Log.i(TAG, "detectUSBName: +++" + volumes);
if (volumes != null) {
for (int i = 0; i < volumes.length; i++) {
String path = (String) getPath.invoke(volumes[i], null);
String label = (String) getLabel.invoke(volumes[i], null);
Log.i(TAG, "detectUSBName Path:" + path + " Label:" + label);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}