简体   繁体   English

如何在android中获取存储在sd卡中的文件名

[英]how to get the file names stored in sd card in android


i have a folder in sd card which contains several files. 我在SD卡中有一个包含多个文件的文件夹。 now i need to get the names of that files. 现在我需要获取该文件的名称。 can anybody have any idea how to get the file names stored in sd card? 任何人都知道如何获取存储在SD卡中的文件名?

any help will be appreciative. 任何帮助都将是值得的。 thanks a lot. 非常感谢。

Environment.getExternalStorageDirectory will give you a File corresponding to the SDCARD. Environment.getExternalStorageDirectory将为您提供与SDCARD相对应的File Then you'll just have to use File methods. 然后你只需要使用File方法。

That should be something like that : 那应该是这样的:

File sdCardRoot = Environment.getExternalStorageDirectory();
File yourDir = new File(sdCardRoot, "yourpath");
for (File f : yourDir.listFiles()) {
    if (f.isFile())
        String name = f.getName();
        // make something with the name
}

A little note of advice : from KitKat and above, this requires the READ_EXTERNAL_STORAGE permission. 一点建议:从KitKat及以上版本,这需要READ_EXTERNAL_STORAGE权限。

/**
 * Return list of files from path. <FileName, FilePath>
 *
 * @param path - The path to directory with images
 * @return Files name and path all files in a directory, that have ext = "jpeg", "jpg","png", "bmp", "gif"  
 */
private List<String> getListOfFiles(String path) {

    File files = new File(path);

    FileFilter filter = new FileFilter() {

        private final List<String> exts = Arrays.asList("jpeg", "jpg",
                "png", "bmp", "gif");

        @Override
        public boolean accept(File pathname) {
            String ext;
            String path = pathname.getPath();
            ext = path.substring(path.lastIndexOf(".") + 1);
            return exts.contains(ext);
        }
    };

    final File [] filesFound = files.listFiles(filter);
    List<String> list = new ArrayList<String>();
    if (filesFound != null && filesFound.length > 0) {
        for (File file : filesFound) {
           list.add(file.getName());
        }
    }

    return list;
}

This will give you the list of images in a folder. 这将为您提供文件夹中的图像列表。 You can modify the code to get all files. 您可以修改代码以获取所有文件。

In Android 5.0 Lollipop, I found that we need add the permission to Manifest: 在Android 5.0 Lollipop中,我发现我们需要向Manifest添加权限:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

If not, we could not see any file in sdcard. 如果没有,我们在SD卡中看不到任何文件。 I take an hour to find this! 我需要一个小时才能找到这个!

 ArrayList<String>nameList = new ArrayList<String>();
 File yourDir = new File(Environment.getExternalStorageDirectory(), "/myFolder");
 for (File f : yourDir.listFiles()) 
 {
    if (f.isFile())
    {
       nameList.add(f.getName);
    }

}

If you want to retrieve all files and folder from a specific path of FOLDER then use this code it will help you 如果要从FOLDER的特定路径检索所有文件和文件夹,请使用此代码它将帮助您

String path="/mnt/sdcard/dcim";  //lets its your path to a FOLDER

String root_sd = Environment.getExternalStorageDirectory().toString();
File file = new File(path) ;       
File list[] = file.listFiles();
  for(File f:list)
    {
       filename.add(f.getName());//add new files name in the list
     }              

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

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