3

I made a simple mp3 player on Android Studio, I know how to get Artist Name and Song Title, but I don't know how to get the Album Cover Image (to set in a ImageView) This is the code that I used to get and display on a ListView the tracks with artist and title

public void getSongList() {
        ContentResolver trackResolver = getContentResolver();
        Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        Cursor trackCursor = trackResolver.query(musicUri, null, null, null, null);


        if(trackCursor!=null && trackCursor.moveToFirst()){
            //get columns
            int titleColumn = trackCursor.getColumnIndex
                    (android.provider.MediaStore.Audio.Media.TITLE);
            int artistColumn = trackCursor.getColumnIndex
                    (android.provider.MediaStore.Audio.Media.ARTIST);
            //add songs to list
            do {
                String thisTitle = trackCursor.getString(titleColumn);
                String thisArtist = trackCursor.getString(artistColumn);
                songList.add(new Song(thisId, thisTitle, thisArtist));
            }
            while (trackCursor.moveToNext());
        }

    }
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Manuel Rizzo
  • 585
  • 6
  • 14
  • 1
    [This might help you.](http://stackoverflow.com/questions/17573972/how-can-i-display-album-art-using-mediastore-audio-albums-album-art) – jack jay May 18 '17 at 07:06

1 Answers1

2

use this to get the uri of the album art cover

public static Uri getArtUriFromMusicFile(Context context, File file) {
    final Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    final String[] cursor_cols = { MediaStore.Audio.Media.ALBUM_ID };

    final String where = MediaStore.Audio.Media.IS_MUSIC + "=1 AND " + MediaStore.Audio.Media.DATA + " = '"
            + file.getAbsolutePath() + "'";
    final Cursor cursor = context.getApplicationContext().getContentResolver().query(uri, cursor_cols, where, null, null);
    /*
     * If the cusor count is greater than 0 then parse the data and get the art id.
     */
    if (cursor != null && cursor.getCount() > 0) {
        cursor.moveToFirst();
        Long albumId = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID));

        Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
        Uri albumArtUri = ContentUris.withAppendedId(sArtworkUri, albumId);
        cursor.close();
        return albumArtUri;
    }
    return Uri.EMPTY;
}
shinilms
  • 1,494
  • 3
  • 22
  • 35