|
public static List<StorageInfo> listAvaliableStorage(Context context) {
storages = new ArrayList<StorageInfo>();
StorageManager storageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
try {
Class<?>[] paramClasses = {};
Method getVolumeList = StorageManager.class.getMethod(
"getVolumeList", paramClasses);
getVolumeList.setAccessible(true);
Object[] params = {};
Object[] invokes = (Object[]) getVolumeList.invoke(storageManager,
params);
if (invokes != null) {
StorageInfo info = null;
for (int i = 0; i < invokes.length; i++) {
Object obj = invokes[i];
Method getPath = obj.getClass().getMethod("getPath",
new Class[0]);
String path = (String) getPath.invoke(obj, new Object[0]);
info = new StorageInfo(path);
Method isRemovable = obj.getClass().getMethod(
"isRemovable", new Class[0]);
String state = null;
try {
Method getVolumeState = StorageManager.class.getMethod(
"getVolumeState", String.class);
state = (String) getVolumeState.invoke(storageManager,
info.path);
info.state = state;
} catch (Exception e) {
e.printStackTrace();
}
if (info.isMounted()) {
info.isRemoveable = ((Boolean) isRemovable.invoke(obj,
new Object[0])).booleanValue();
storages.add(info);
}
}
}
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException | IllegalArgumentException e1) {
e1.printStackTrace();
}
sdCounts = storages.size();
return storages;
} |
|