list Root Directories - Android java.io

Android examples for java.io:Directory

Description

list Root Directories

Demo Code

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;

public class Main {

  public static List<File> listRootDirs(Context context) {
    File[] filesDirs = new File[] { context.getExternalFilesDir(null) };

    List<File> rootDirs = new ArrayList<>(filesDirs.length);
    for (File path : filesDirs) {
      File root = getFSRootForPath(path);
      if (root != null)
        rootDirs.add(root);//w  w  w. j  a  v a  2 s  .  c o m
    }
    return rootDirs;
  }

  private static File getFSRootForPath(File path) {
    while (path != null && path.isDirectory()) {
      long fsSize = path.getTotalSpace();
      File parent = path.getParentFile();
      if (parent == null || parent.getTotalSpace() != fsSize)
        return path;
      path = parent;
    }
    return path;
  }

}

Related Tutorials