android 获取所有SD卡目录

//返回sd卡路径
public static List<String> getStorageDirectories(Context context) {
StorageManager sm = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
List<String> pathList = new ArrayList<>();
File exDirFile = Environment.getExternalStorageDirectory();
String externalStorageDir = "";
if (exDirFile != null) {
externalStorageDir = exDirFile.getAbsolutePath();
}
try {
String[] paths = (String[]) sm.getClass().getMethod("getVolumePaths", null).invoke(sm, null);
if (paths != null && paths.length > 0) {
for (String path : paths) {
File file = new File(path);
if (!TextUtils.isEmpty(path) && !path.equalsIgnoreCase(externalStorageDir) && file.isDirectory()) {
//判断是否目录,并排除系统虚拟出来的目录
pathList.add(path);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}

return pathList;
}

posted on 2017-10-20 11:17  guangdeshishe  阅读(303)  评论(0编辑  收藏  举报

导航