Skip to content

Instantly share code, notes, and snippets.

@kwong93
Created September 7, 2017 20:35
Show Gist options
  • Save kwong93/eed0244c0d2202aa7adfdd05fef14735 to your computer and use it in GitHub Desktop.
Save kwong93/eed0244c0d2202aa7adfdd05fef14735 to your computer and use it in GitHub Desktop.
Android how to get all external directory files including sd card
public List<File> getExternalDirectoryRoots(Context context) {
File[] externalFilesDirs = ContextCompat.getExternalFilesDirs(context, null);
List<File> externalDirectories = new ArrayList<>();
Set<String> stringSet = new HashSet<>();
for (File file : externalFilesDirs) {
String[] split = file.getAbsolutePath().split("/");
if (split.length > 1) {
stringSet.add(split[1]);
}
}
for (String str : stringSet) {
externalDirectories.add(new File(str));
}
return externalDirectories;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment