2015-11-05 54 views
0

如何獲得「封面歌曲」(所以同一張專輯中的歌曲有相同的封面)?如何獲取唱片封面查詢歌曲

// Retrieve song info from device 
public void getSongList() { 
    String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0"; 

    // Query external audio 
    ContentResolver musicResolver = getActivity().getContentResolver(); 
    Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; 
    Cursor musicCursor = musicResolver.query(musicUri, null, selection, null, null); 

    // Iterate over results if valid 
    if (musicCursor != null && musicCursor.moveToFirst()) { 
     // Get columns 
     int titleColumn = musicCursor.getColumnIndex 
       (android.provider.MediaStore.Audio.Media.TITLE); 
     int idColumn = musicCursor.getColumnIndex 
       (android.provider.MediaStore.Audio.Media._ID); 
     int artistColumn = musicCursor.getColumnIndex 
       (android.provider.MediaStore.Audio.Media.ARTIST); 
     int durationColumn = musicCursor.getColumnIndex 
       (MediaStore.Audio.Media.DURATION); 

     // Add songs to list 
     do { 
      long thisId = musicCursor.getLong(idColumn); 
      String thisTitle = musicCursor.getString(titleColumn); 
      String thisArtist = musicCursor.getString(artistColumn); 
      long thisDuration = musicCursor.getLong(durationColumn); 

      String thisPathAlbumImage = ???? 
      //**** HERE I WANT A PATH/URI WITH ALBUM SONG **** 

      arrayOfSongs.add(new Song(thisId, thisTitle, thisArtist, thisDuration, thisPathAlbumImage)); 

      Log.d(LOG_TAG, "New song added: " + thisTitle); 
     } 
     while (musicCursor.moveToNext()); 
    } 
} 

我需要另一個查詢嗎?我不想要關於所有專輯的查詢,我不知道如何將歌曲連接到專輯...

回答

1

您需要在上面的查詢中包含專輯ID。

int albumIdColumn = musicCursor.getColumnIndex 
      (MediaStore.Audio.Media.ALBUM_ID); 

long albumId = musicCursor.getLong(albumIdColumn); 

隨着專輯的ID,你可以查詢蓋這樣的路徑:

private static String getCoverArtPath(long albumId, Context context) { 
    Cursor albumCursor = context.getContentResolver().query(
      MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, 
      new String[]{MediaStore.Audio.Albums.ALBUM_ART}, 
      MediaStore.Audio.Albums._ID + " = ?", 
      new String[]{Long.toString(albumId)}, 
      null 
    ); 
    boolean queryResult = albumCursor.moveToFirst(); 
    String result = null; 
    if (queryResult) { 
     result = albumCursor.getString(0); 
    } 
    albumCursor.close(); 
    return result; 
}