Scan a file folder in android for file paths

后端 未结 2 869
南笙
南笙 2021-02-06 03:21

So i have a folder at \"mnt/sdcard/folder\" and its filled with image files. I want to be able to scan the folder and for each of the files that is in the folder put each file p

相关标签:
2条回答
  • 2021-02-06 03:43

    Yes, you can use the java.io.File API with FileFilter.

    File dir = new File(path);
    FileFilter filter = new FileFilter() {
        @Override
        public boolean accept(File file) {
            return file.getAbsolutePath().matches(".*\\.png");
        }
    };
    File[] images = dir.listFiles(filter);
    

    I was quite surprised when I saw this technique, as it's quite easy to use and makes for readable code.

    0 讨论(0)
  • 2021-02-06 04:05

    You could use

    List<String> paths = new ArrayList<String>();
    File directory = new File("/mnt/sdcard/folder");
    
    File[] files = directory.listFiles();
    
    for (int i = 0; i < files.length; ++i) {
        paths.add(files[i].getAbsolutePath());
    }
    

    See listFiles() variants in File (one empty, one FileFilter and one FilenameFilter).

    0 讨论(0)
提交回复
热议问题