简体   繁体   English

Android SD卡目录文件夹名称列表

[英]Android SD card directory folders name list

How can I get names of all folders (not files) in specific directory on SD card? 如何获取SD卡上特定目录中所有文件夹(不是文件)的名称? For example all subfolders names in /sdcard/first_level_folder/... . 例如, /sdcard/first_level_folder/...所有子文件夹名称。

I need folder name, so I can pass complete path (string) to the method which will then compress (zip) it. 我需要文件夹名称,所以我可以将完整路径(字符串)传递给方法,然后压缩(zip)它。

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. 步骤1:使用Environment.getExternalStorageDirectory()获取外部存储的根目录。 /sdcard/ is incorrect on most devices. /sdcard/在大多数设备上都不正确。

Step #2: Use the appropriate File constructor to create the File object pointing to your desired directory inside external storage. 步骤2:使用适当的File构造函数创建指向外部存储内所需目录的File对象。

Step #3: Use Java I/O to find what is in that directory. 步骤#3:使用Java I / O查找该目录中的内容。

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. 这是一个与java相关的问题。 Check this out. 看看这个

To get access to the sdcard folder name : 要访问sdcard文件夹名称:

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. 步骤1:使用Environment.getExternalStorageDirectory()获取外部存储的根目录。 /sdcard/ is incorrect on most devices. /sdcard/在大多数设备上都不正确。

Step #2: Use the appropriate File constructor to create the File object pointing to your desired directory inside external storage. 步骤2:使用适当的File构造函数创建指向外部存储内所需目录的File对象。

Step #3: Use Java I/O to find what is in that directory. 步骤#3:使用Java I / O查找该目录中的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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