JAVA ANDROID - get list files of a folder

后端 未结 2 760
花落未央
花落未央 2021-01-15 03:28

I want to display images of different folders in a GridView, but I don\'t know what I need to do for get a list with the name of files that are inside a folder

相关标签:
2条回答
  • 2021-01-15 04:09

    this method will give you a list containing a list of dir folders and also sub folders:

    public void listf(String directoryName, ArrayList<File> files) {
    File directory = new File(directoryName);
    
    // get all the files from a directory
    File[] fList = directory.listFiles();
    for (File file : fList) {
        if (file.isFile()) {
            files.add(file);
        } else if (file.isDirectory()) {
            listf(file.getAbsolutePath(), files);
        }
    }
    }
    
    0 讨论(0)
  • 2021-01-15 04:22

    First Wirte this method in AsyncTask class.

    private List<File> getListFiles(File parentDir) {
            ArrayList<File> inFiles = new ArrayList<File>();
            File[] files = parentDir.listFiles();
            for (File file : files) {
                if (file.isDirectory()) {
                    if(file.getName().toString().toLowerCase().contains(fileNameToSearch.toLowerCase()) || file.getName().toString().toUpperCase().contains(fileNameToSearch.toUpperCase())){
                        inFiles.add(file);
                    }else {
                        inFiles.addAll(getListFiles(file));
                    }
                } else {
                    if(file.getName().toString().toLowerCase().contains(fileNameToSearch.toLowerCase()) || file.getName().toString().toUpperCase().contains(fileNameToSearch.toUpperCase())){
                        inFiles.add(file);
                    }
                }
            }
            return inFiles;
        }
    

    Call this method from doInBackground(...)

    List<File> files = getListFiles(new File(directoryPath));
    
    0 讨论(0)
提交回复
热议问题