Reputation: 22291
How to get a list of all the Directories on your SD card?
Upvotes: 1
Views: 1505
Reputation: 1013
Please the try the following code.
Code :
System.out.println(" File List Start" );
File file[] = Environment.getExternalStorageDirectory().listFiles();
for (int i=1;i<=file.length;i++){
System.out.println(" No. "+i+" : "+file[i-1].getAbsolutePath());
}
System.out.println(" File List End" );
Upvotes: 0
Reputation: 3770
By All Directories i think you mean even the child directories in the sdcard, well if yes then heres what you need to do
1) Initialize an ArrayList
private ArrayList<String> allDirectories = new ArrayList<String>();
2) Copy and Paste this function:
public void listAllDirectories(String path) {
File tempfile = new File(path);
File[] files = tempfile.listFiles();
if (files != null) {
for (File checkFile : files) {
if (checkFile.isDirectory()) {
allDirectories.add(checkFile.getName());
listAllDirectories(checkFile.getAbsolutePath());
}
}
}
}
3) Call it from wherever you want
listAllDirectories(Environment.getExternalStorageDirectory().toString());
for (int i = 0; i < allDirectories.size(); i++) {
System.out.println(allDirectories.get(i));
}
Upvotes: 1
Reputation: 53687
Try with the following code to get list if directories
public static ArrayList<String> getAllDirectoriesFromSDCard(Activity activity) {
ArrayList<String> absolutePathOfImageList = new ArrayList<String>();
File file[] = Environment.getExternalStorageDirectory().listFiles();
for (File f : file)
{
if (f.isDirectory()) {
absolutePathOfImageList.add(f.getAbsolutePath());
}
}
// Log.i(TAG,"........Detected images for Grid....."
// + absolutePathOfImageList);
return absolutePathOfImageList;
}
Upvotes: 2