Skip to content

Instantly share code, notes, and snippets.

@zi6xuan
Created October 12, 2018 05:20
Show Gist options
  • Save zi6xuan/cdf06f9768168b93087429f2fdc38f1c to your computer and use it in GitHub Desktop.
Save zi6xuan/cdf06f9768168b93087429f2fdc38f1c to your computer and use it in GitHub Desktop.
获得所有sd卡挂载目录
public class StorageUtils {
public static ArrayList<Volume> getVolume(Context context) {
ArrayList<Volume> list_storagevolume = new ArrayList<Volume>();
StorageManager storageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
try {
Method method_volumeList = StorageManager.class.getMethod("getVolumeList");
method_volumeList.setAccessible(true);
Object[] volumeList = (Object[]) method_volumeList.invoke(storageManager);
if (volumeList != null) {
Volume volume;
for (Object aVolume : volumeList) {
try {
volume = new Volume();
volume.setPath((String) aVolume.getClass().getMethod("getPath").invoke(aVolume));
volume.setRemovable((boolean) aVolume.getClass().getMethod("isRemovable").invoke(aVolume));
volume.setState((String) aVolume.getClass().getMethod("getState").invoke(aVolume));
list_storagevolume.add(volume);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
} else {
Log.e("null", "null-------------------------------------");
}
} catch (Exception e1) {
e1.printStackTrace();
}
return list_storagevolume;
}
/*
存储设备信息封装类
*/
public static class Volume {
protected String path;
protected boolean removable;
protected String state;
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public boolean isRemovable() {
return removable;
}
public void setRemovable(boolean removable) {
this.removable = removable;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment