Reputation: 87
How can I get names of all folders (not files) in specific directory on SD card? For example all subfolders names in /sdcard/first_level_folder/...
.
I need folder name, so I can pass complete path (string) to the method which will then compress (zip) it.
Thanks.
Upvotes: 5
Views: 19064
Reputation: 13801
I think what you are looking for is
File dir = new File("directoryPath");
FileFilter fileFilter = new FileFilter() {
public boolean accept(File file) {
return file.isDirectory();
}
};
File[] files = dir.listFiles(fileFilter);
Upvotes: 12
Reputation: 383
File file[] = Environment.getExternalStorageDirectory().listFiles();
for (File f : file) {
if (f.isDirectory()) {
// ... do stuff
}
}
Step #1: Use Environment.getExternalStorageDirectory()
to get the root of external storage. /sdcard/
is incorrect on most devices.
Step #2: Use the appropriate File
constructor to create the File
object pointing to your desired directory inside external storage.
Step #3: Use Java I/O to find what is in that directory.
Upvotes: -2
Reputation: 3329
File file=new File("/mnt/sdcard/");
File[] list = file.listFiles();
int count = 0;
for (File f: list){
String name = f.getName();
if (name.endsWith(".jpg") || name.endsWith(".mp3") || name.endsWith(".some media extention"))
count++;
System.out.println("170 " + count);
}
Upvotes: 0
Reputation: 9252
Well you can use something like:
File file[] = Environment.getExternalStorageDirectory().listFiles();
for (File f : file)
{
if (f.isDirectory()) { ... do stuff }
}
Upvotes: 3
Reputation: 9300
well this is a java related question. Check this out.
To get access to the sdcard folder name :
File extStore = Environment.getExternalStorageDirectory();
String SD_PATH = extStore.getAbsolutePath()+"your sd folder here";
Upvotes: 2
Reputation: 1007554
Step #1: Use Environment.getExternalStorageDirectory()
to get the root of external storage. /sdcard/
is incorrect on most devices.
Step #2: Use the appropriate File
constructor to create the File
object pointing to your desired directory inside external storage.
Step #3: Use Java I/O to find what is in that directory.
Upvotes: 8