Reputation: 615
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());
}
}
Upvotes: 3
Views: 3396
Reputation: 1493
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;
}
Upvotes: 2