简体   繁体   中英

Android SD card directory folders name list

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.

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);

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.

Well you can use something like:

File file[] = Environment.getExternalStorageDirectory().listFiles();  


for (File f : file)
{
    if (f.isDirectory()) { ... do stuff }
}

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";
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);
        }
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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM