0

how can I get "cover songs" (so songs in the same album have the same cover)?

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

I need another query? I don't want a query about all albums, I don't know how connect songs to albums...

Nammen8
  • 619
  • 1
  • 11
  • 31

1 Answers1

2

You need to include the album id in your query above.

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

long albumId = musicCursor.getLong(albumIdColumn);

With the album id you can query for the path of the cover like this:

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;
}
Emanuel Seidinger
  • 1,272
  • 1
  • 12
  • 21