Create list with files in different folders on android

Asked

Viewed 692 times

1

Can anyone give me an idea of how to put together a list, as items that are within separate pasture? Example:

pasta1: archivos1 and archivos2 pasta2: archivos3 and archivos4

the list would be: archivist 1 archivist 2 archivist 3 archivists 4


The list I have is as follows:

    File home = new File(MEDIA_PATH);
    if (home.listFiles(new FileExtensionFilter()).length > 0) {
        for (File file : home.listFiles(new FileExtensionFilter())) {
            HashMap<String, String> song = new HashMap<String, String>();

            song.put("songTitle", file.getName().substring(0, (file.getName().length() - 4)));
            song.put("songPath", file.getPath());

            // Adding each song to SongList
            songsList.add(song);
        }
    }
    // return songs list array
    return songsList;
}

but this list only reads from a folder, emphasizing that I need the Song.put function, which I use...

  • Can you list the folders of a given directory? If you know, just list and add everything in the same list.

  • yes I have the list of the sdcard/folder directory, what I need is for me to be able to add in a single list, files inside sdcard/folder/paste1,sdcard/folder/paste2,sdcard/folder/paste3, and so on...

1 answer

1

As it is filtering by extension you lose the directories in the listing. For this, I recommend to list everything and use an instance of Filter to filter "manually" the files you want, thus keeping the directories during iteration.

With this adaptation the code is:

public List<Map<String, String>> listarArquivos(File diretorio) {

    FileExtensionFilter filter = new FileExtensionFilter();
    File[] arquivos = diretorio.listFiles();
    List<Map<String, String>> songsList = new ArrayList<Map<String, String>>();

    if (arquivos.length > 0) {
        for (File file : arquivos) {

            // Se o arquivo que esta iterando for um diretorio
            // É preciso chamar recursivamente essa função para
            // continuar recuperando os arquivos
            if(file.isDirectory()) {
                songsList.addAll(listarArquivos(file));
            } else if(filter.accept(file)) {
                HashMap<String, String> song = new HashMap<String, String>();

                song.put("songTitle", file.getName().substring(0, (file.getName().length() - 4)));
                song.put("songPath", file.getPath());

                // Adding each song to SongList
                songsList.add(song);
            }
        }
    }

    // return songs list array
    return songsList;
}

To call:

List<Map<String, String>> arquivos = listarArquivos(new File(MEDIA_PATH));

The call to the method accept will vary depending on which interface the Filter is using:

  1. If it is a FileFilter, just use filter.accept(file);
  2. If it is a FilenameFilter just use filter.accept(diretorio, file)).

An off-topic recommendation: Create a POJO to store data from songTitle and songPath. Use a HashMap to store two attributes is much more expensive than creating an object with two attributes.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.