android scanning all .mp3 files in SD Card

前端 未结 5 766

I\'m trying to scan all .mp3 files in my SD card and save its name. here is a fragment of code which is responsible for that. 1.What i\'m doing wrong? 1. Which is correct

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-16 03:14

    I'm not sure what's wrong in your code. But you can give this approach a try:

    public HashMap scanDirectory(File dir) {
    
    HashMap song; 
    String mp3Pattern = ".mp3";
    File listFile[] = dir.listFiles();
    
    if (listFile != null) {
        for (int i = 0; i < listFile.length; i++) {
    
            if (listFile[i].isDirectory()) {
                walkdir(listFile[i]);
            } else {
              if (listFile[i].getName().endsWith(mp3Pattern)){
                   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 song;
      }
    

    To scan entire whole sdcard for mp3 call scanDirectory(Environment.getExternalStorageDirectory());

提交回复
热议问题