How to display files on the SD card in a ListView?

前端 未结 6 2111
遥遥无期
遥遥无期 2020-12-03 06:24

I would like to create a button that when clicked will go to a class that displays all media files from an SD card using a ListView.

After selecting fro

6条回答
  •  无人及你
    2020-12-03 06:43

    this is a correct solution for you! or i give you a link for this stuff!

    public class SongsManager {
    // SDCard Path
    //choose your path for me i choose sdcard
    final String MEDIA_PATH = new String("/sdcard/");
    private ArrayList> songsList = new ArrayList>();
    
    // Constructor
    public SongsManager(){
    
    }
    
    /**
     * Function to read all mp3 files from sdcard
     * and store the details in ArrayList
     * */
    public ArrayList> getPlayList(){
        File home = new File(MEDIA_PATH);
    
        if (home.listFiles(new FileExtensionFilter()).length > 0) {
            for (File file : home.listFiles(new FileExtensionFilter())) {
                HashMap song = new HashMap();
                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;
    }
    
    /**
     * Class to filter files which are having .mp3 extension
     * */
    //you can choose the filter for me i put .mp3
    class FileExtensionFilter implements FilenameFilter {
        public boolean accept(File dir, String name) {
            return (name.endsWith(".mp3") || name.endsWith(".MP3"));
        }
    }
     }
    

提交回复
热议问题