2016-09-13 36 views
1

我正在開發Android中的音樂播放器,但一直在閱讀MP3文件。 這裏是我的代碼來讀取所有的mp3文件。但它沒有迴應任何設備的文件(雖然有一些文件,我使用adb複製)。我也希望它使用專輯,藝術家等列出請幫助。列出Android中的所有Mp3文件

final String MEDIA_PATH = Environment.getExternalStorageDirectory()+""; 

private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>(); 

// Constructor 
public SongsManager(){ 

} 

/** 
* Function to read all mp3 files from sdcard 
* and store the details in ArrayList 
* */ 
public ArrayList<HashMap<String, String>> getPlayList(){ 
    File home = new File(MEDIA_PATH); 

    //if (home.listFiles(new FileExtensionFilter()).length > 0) { 
    if (home.listFiles(new FileExtensionFilter())!=null) { 

     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; 
} 

/** 
* Class to filter files which are having .mp3 extension 
* */ 
class FileExtensionFilter implements FilenameFilter { 
    public boolean accept(File dir, String name) { 
     return (name.endsWith(".mp3") || name.endsWith(".MP3")); 
    } 
} 
+0

試試這個http://stackoverflow.com/a/38239152/3946555,從這個鏈接你會發現如何從手機內存和SD卡內存中獲得「.mp3」文件。希望這會幫助你 – Madhan

+0

如果你想列出歌曲作爲專輯(或)藝術家明智。你應該爲MediaStore ContentProvider。這裏是一個官方文檔https://developer.android.com/reference/android/provider/MediaStore.html – Madhan

+0

嗨@Madhan謝謝你mesg,我試試這個,但同樣..你可以plz幫助我。 –

回答

6

這裏我已經修改了你的getPlayList()方法。瞭解更多。

ArrayList<HashMap<String,String>> getPlayList(String rootPath) { 
      ArrayList<HashMap<String,String>> fileList = new ArrayList<>(); 


      try { 
       File rootFolder = new File(rootPath); 
       File[] files = rootFolder.listFiles(); //here you will get NPE if directory doesn't contains any file,handle it like this. 
       for (File file : files) { 
        if (file.isDirectory()) { 
         if (getPlayList(file.getAbsolutePath()) != null) { 
          fileList.addAll(getPlayList(file.getAbsolutePath())); 
         } else { 
          break; 
         } 
        } else if (file.getName().endsWith(".mp3")) { 
         HashMap<String, String> song = new HashMap<>(); 
         song.put("file_path", file.getAbsolutePath()); 
         song.put("file_name", file.getName()); 
         fileList.add(song); 
        } 
       } 
       return fileList; 
      } catch (Exception e) { 
       return null; 
      } 
     } 

你可以得到歌曲的名字和歌曲的路徑是這樣的:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main_activity); 
ArrayList<HashMap<String,String>> songList=getPlayList("/storage/sdcard1/"); 
     if(songList!=null){ 
     for(int i=0;i<songList.size();i++){ 
     String fileName=songList.get(i).get("file_name"); 
     String filePath=songList.get(i).get("file_path"); 
     //here you will get list of file name and file path that present in your device 
     log.e("file details "," name ="+fileName +" path = "+filePath); 
     } 
     } 
    } 

注:使用 「/存儲/ sdcard1 /」 從SD卡和使用Environment.getExternalStorageDirectory讀取文件().getAbsolutePath()用於從手機內存中讀取文件

希望這會對您有所幫助。

+0

非常感謝你。它的作品適合我。你已經救了我這麼多小時......再次感謝你。 –

+0

Hi @Madhan嗨,我再次讓你煩惱。我們可以限制遞歸直到3或5個文件夾。它循環到所有子文件夾並花費很多時間。我試圖停止3名傳票後搜索但它不工作。謝謝。 –

+0

如果獲取所有數據意味着花費太多時間,則在單獨的線程中運行此方法。不要在沒有任何正當理由的情況下限制它。您可以使用AsyncTask類來解決此問題 – Madhan

3

嘗試這個

String path; 
File sdCardRoot = Environment.getExternalStorageDirectory(); 
File dir = new File(sdCardRoot.getAbsolutePath() + "/yourDirectory/"); 

if (dir.exists()) { 

    if (dir.listFiles() != null) { 
     for (File f : dir.listFiles()) { 
      if (f.isFile()) 
       path = f.getName(); 

      if (path.contains(".mp3")) { 
       yourArrayList.add(path); 

      } 
     } 
    } 
} 
+0

無法正常工作。在這裏指定空值null。 ** if(dir.listFiles()!= null){** –

+0

這意味着該文件夾中沒有mp3文件...嘗試更改文件夾名稱並檢查dir是否爲空 – AbhayBohra

+0

@kushalPatil要顯示全部來自手機的文件,而不是在特定的文件夾中。所以這個邏輯不起作用。 – Madhan

1

希望你已經找到你的答案,但可能這是更好的,如果你想嘗試一下,這裏是您的解決方案使用下面的代碼來讀取特定文件夾或所有文件MP3文件,

第一所有創建1模型類如下給出,以GETSET列表中的文件。

AudioModel.class

public class AudioModel { 

    String aPath; 
    String aName; 
    String aAlbum; 
    String aArtist; 

    public String getaPath() { 
     return aPath; 
    } 

    public void setaPath(String aPath) { 
     this.aPath = aPath; 
    } 

    public String getaName() { 
     return aName; 
    } 

    public void setaName(String aName) { 
     this.aName = aName; 
    } 

    public String getaAlbum() { 
     return aAlbum; 
    } 

    public void setaAlbum(String aAlbum) { 
     this.aAlbum = aAlbum; 
    } 

    public String getaArtist() { 
     return aArtist; 
    } 

    public void setaArtist(String aArtist) { 
     this.aArtist = aArtist; 
    } 
} 

現在,我們有我們的模型類,使用下面的代碼來閱讀您的文件夾或設備中的所有MP3文件。

這將返回所有MP3文件的列表與音樂NAME, PATH, ARTIST, ALBUM,如果你想要更詳細請參考Media.Store.Audio DOC .. https://developer.android.com/reference/android/provider/MediaStore.Audio.html

public List<AudioModel> getAllAudioFromDevice(final Context context) { 

     final List<AudioModel> tempAudioList = new ArrayList<>(); 

     Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; 
     String[] projection = {MediaStore.Audio.AudioColumns.DATA, MediaStore.Audio.AudioColumns.ALBUM, MediaStore.Audio.ArtistColumns.ARTIST,}; 
     Cursor c = context.getContentResolver().query(uri, projection, MediaStore.Audio.Media.DATA + " like ? ", new String[]{"%yourFolderName%"}, null); 

     if (c != null) { 
      while (c.moveToNext()) { 

       AudioModel audioModel = new AudioModel(); 
       String path = c.getString(0); 
       String album = c.getString(1); 
       String artist = c.getString(2); 

       String name = path.substring(path.lastIndexOf("/") + 1); 

       audioModel.setaName(name); 
       audioModel.setaAlbum(album); 
       audioModel.setaArtist(artist); 
       audioModel.setaPath(path); 

       Log.e("Name :" + name, " Album :" + album); 
       Log.e("Path :" + path, " Artist :" + artist); 

       tempAudioList.add(audioModel); 
      } 
      c.close(); 
     } 

     return tempAudioList; 
    } 

來讀取特定文件夾中的文件,使用下面的查詢和在查詢寫你的文件夾名..

Cursor c = context.getContentResolver().query(uri, 
              projection, 
              MediaStore.Audio.Media.DATA + " like ? ", 
              new String[]{"%yourFolderName%"}, // yourFolderName 
              null); 

如果你想查詢以下設備中使用的所有文件..

Cursor c = context.getContentResolver().query(uri, 
              projection, 
              null, 
              null, 
              null); 

不要忘記添加存儲許可..享受。

+0

哇感謝這是超快速的,將最後的查詢給SD卡中的文件? – Redman

+0

更改此行來從DIFF導演數據: **開放的URI = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; ** **烏里URI = MediaStore.Audio.Media.INTERNAL_CONTENT_URI; ** –

+0

'MediaStore.Audio .Media.EXTERNAL_CONTENT_URI'就夠了我猜,它的清單內部和SD卡 – Redman