2013-10-20 40 views
2

我已經有這個代碼...但我想搜索也通過子文件夾,而不是隻搜索槽sdcard.sorry爲我的英語;) 感謝幫助,vinzenz如何找到SDCard上的所有音頻文件(eclipse,安卓)

public class SongsManager { 
// SDCard Path 
final String MEDIA_PATH = new String("/sdcard/"); 
private ArrayList<HashMap<String, String>> songsList = new  ArrayList<HashMap<String, String>>(); 

// Constructor 
public SongsManager(){ 
    **strong text** 
} 

/** 
* 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) { 
     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; 
} 

}

回答

4

此代碼可以幫助你。它搜索SD卡中的歌曲(甚至在子文件夾中)&將詳細信息存儲在songsList中。

ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>(); 
    String[] STAR = { "*" }; 

    Cursor cursor; 
    Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; 
    String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0"; 


    cursor = managedQuery(uri, STAR, selection, null, null); 

    if (cursor != null) { 
     if (cursor.moveToFirst()) { 
      do { 
       String songName = cursor 
         .getString(cursor 
           .getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME)); 


       String path = cursor.getString(cursor 
         .getColumnIndex(MediaStore.Audio.Media.DATA)); 


       String albumName = cursor.getString(cursor 
         .getColumnIndex(MediaStore.Audio.Media.ALBUM)); 
       int albumId = cursor 
         .getInt(cursor 
           .getColumnIndex(MediaStore.Audio.Media.ALBUM_ID)); 

       HashMap<String, String> song = new HashMap<String, String>(); 
       song.put("songTitle",albumName+" "+songName+"___"+albumId); 
       song.put("songPath",path); 
       songsList.add(song); 

      } while (cursor.moveToNext()); 


     } 

    } 
+1

感謝一個很好的答案,本書雖然該方法managedQuery已棄用,請問您可以更新此答案嗎? – Marcus

+1

而不是已棄用的managedQuery(),可以使用具有相同參數的getContentResolver()。query()。 – San

0

沒有直接的解決你的問題 你寫一段代碼,通過目錄中的每個文件進行迭代,如果當前文件是那麼的目錄列出目錄下的所有文件,並再次重複它的所有文件等等。
類似下面的代碼將有助於在本thread

import java.io.File; 

public class DirectoryReader { 

    static int spc_count=-1; 

    static void process(File aFile) { 
    spc_count++; 

    if(aFile.isFile()) { 
     // your logic to check songs file 
    } else if (aFile.isDirectory()) { 

     File[] listOfFiles = aFile.listFiles(); 
     if(listOfFiles!=null) { 
     for (int i = 0; i < listOfFiles.length; i++) { 
      process(listOfFiles[i]); 
     } 
     } else { 
     // access is denied 
     } 
    } 

    spc_count--; 
    } 

    public static void main(String[] args) { 
    String nam = "/sdcard/"; 
    File aFile = new File(nam); 
    process(aFile); 
    } 

} 
2

考慮到媒體,從SD卡進行掃描,就可以從MediaStore得到這個信息

musiccursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, 
      proj, MediaStore.Audio.Media.MIME_TYPE + "= audio/mpeg", null, null); 

int column_index = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA); 


while(musiccursor.moveToNext()){ 

String path = musiccursor.get(column_index); 
//path is your music path, use this 
}