標籤:android style blog color 使用 檔案 資料 io
當寫一些東西(比如音樂播放器)的時候,就需要SD卡的mp3列表了~開始我是使用列檔案來著,但是再擷取檔案的資訊(歌手,專輯,圖片,時間長度等。。)很麻煩,所以直接用這個函數了~
1 private void getMusicPaths(){ 2 // 3 // 擷取音樂列表 4 // 5 Cursor cursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, MediaStore.Audio.Media.DEFAULT_SORT_ORDER); 6 if(cursor.moveToFirst()){ 7 while (!cursor.isAfterLast()) { 8 //歌曲編號 9 //int id = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID)); 10 //歌曲標題11 String tilte = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE)); 12 //歌曲的專輯名:MediaStore.Audio.Media.ALBUM13 //String album = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM)); 14 //歌曲的歌手名: MediaStore.Audio.Media.ARTIST15 String artist = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST)); 16 //歌曲檔案的路徑 :MediaStore.Audio.Media.DATA17 String url = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA)); 18 //歌曲的總播放時間長度 :MediaStore.Audio.Media.DURATION19 //int duration = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION)); 20 //歌曲檔案的大小 :MediaStore.Audio.Media.SIZE21 //Long size = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE));22 cursor.moveToNext(); 23 } 24 }25 }