2011-03-14 59 views
2

例如,我想要檢索內部和外部存儲上所有.mp3文件的列表。我還想每個.mp3文件的路徑(String)作爲參考(將它們存儲在List?)我怎麼能這樣做?這是一個相對昂貴的操作?如果是這樣,它應該放在一個線程上,並在運行時,向用戶提供一個「加載」列出Android設備上的所有文件類型?

回答

6

請找到下面的函數。它將獲得存儲在外部和內部存儲器中的所有mp3文件。

public void getAllSongs() { 

    Uri allsongsuri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; 
    String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0"; 

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

    if (cursor != null) { 
     if (cursor.moveToFirst()) { 
      do { 
       song_name = cursor 
         .getString(cursor 
           .getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME)); 
       int song_id = cursor.getInt(cursor 
         .getColumnIndex(MediaStore.Audio.Media._ID)); 

       String fullpath = cursor.getString(cursor 
         .getColumnIndex(MediaStore.Audio.Media.DATA)); 
       fullsongpath.add(fullpath); 

       album_name = cursor.getString(cursor 
         .getColumnIndex(MediaStore.Audio.Media.ALBUM)); 
       int album_id = cursor.getInt(cursor 
         .getColumnIndex(MediaStore.Audio.Media.ALBUM_ID)); 

       artist_name = cursor.getString(cursor 
         .getColumnIndex(MediaStore.Audio.Media.ARTIST)); 
       int artist_id = cursor.getInt(cursor 
         .getColumnIndex(MediaStore.Audio.Media.ARTIST_ID)); 


      } while (cursor.moveToNext()); 

     } 
     cursor.close(); 
     db.closeDatabase(); 
    } 
}