Skip to content

Instantly share code, notes, and snippets.

@uchidev
Last active February 17, 2016 15:42
Show Gist options
  • Select an option

  • Save uchidev/9fe251150ec85baf1d89 to your computer and use it in GitHub Desktop.

Select an option

Save uchidev/9fe251150ec85baf1d89 to your computer and use it in GitHub Desktop.
ExternalStorage operation
private class ExternalStorage {
public List<String> getDirPathList() {
try {
return getDirPathList(Environment.getExternalStorageDirectory().getCanonicalFile());
} catch (IOException e) {
return emptyList();
}
}
public List<String>getDirPathList(File dir) {
List<String> resultDirPathList = emptyList();
if (dir != null) {
List<String> dirPathList = getJustBelowDirPathList(dir);
for (String path : dirPathList) {
if (path != null) {
resultDirPathList.add(path);
resultDirPathList.addAll(getDirPathList(new File(path)));
}
}
}
return resultDirPathList;
}
public List<String>getJustBelowDirPathList(File dir) {
List<String> dirPathList = new ArrayList<>();
if (dir != null && dir.isDirectory() && !dir.isHidden() && dir.canRead()) {
for (String nodeName: dir.list()) {
if (nodeName != null) {
File node = new File(dir, nodeName);
if (isAvailableDir(node)) {
dirPathList.add(node.getPath());
}
}
}
}
return dirPathList;
}
private boolean isAvailableDir(File node) {
return (node != null
&& node.isDirectory()
&& !node.isHidden()
&& node.canRead()
&& !hasNoMediaFile(node)
&& hasCanonicalPathName(node));
}
private boolean hasNoMediaFile(File node) {
File file = new File(node, NO_MEDIA_FILE);
return file.exists();
}
private boolean hasCanonicalPathName(File file) {
try {
return file.getPath().equals(file.getCanonicalPath());
} catch (IOException e) {
return false;
}
}
private List<String> emptyList() {
return new ArrayList<>();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment