Cover Art on Android

cover art on android

Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id);
ContentResolver res = context.getContentResolver();
InputStream in = res.openInputStream(uri);
Bitmap artwork = BitmapFactory.decodeStream(in);

More complete sample code can be found in Android Music player source here https://github.com/android/platform_packages_apps_music/blob/master/src/com/android/music/MusicUtils.java method getArtworkQuick.

load cover audio art in android

Solved:

  • Load audio file:
 private void loadAudio() {
ContentResolver contentResolver = getContentResolver();

Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection = MediaStore.Audio.Media.IS_MUSIC + "!= 0";
String sortOrder = MediaStore.Audio.Media.ARTIST + " DESC";
Cursor cursor = contentResolver.query(uri, null, selection, null, sortOrder);

if (cursor != null && cursor.getCount() > 0) {
audioList.clear();
while (cursor.moveToNext()) {
Audio audio = new Audio();
String data = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
String title = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
String album = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));
String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
int albumId = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
Bitmap thumbnail = null;
String albumArt = null;
Uri imageUri_t;

Cursor cursor1 = contentResolver.query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
new String[]{MediaStore.Audio.Albums._ID, MediaStore.Audio.Albums.ALBUM_ART},
MediaStore.Audio.Albums._ID + "=" + albumId, null, null);

while (cursor1.moveToNext()) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
Size thumbSize = new Size(100, 100);
try {
int thumbColumn = cursor1.getColumnIndexOrThrow(MediaStore.Audio.Albums._ID);
int _thumpId = cursor1.getInt(thumbColumn);
imageUri_t = ContentUris.withAppendedId(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, _thumpId);
thumbnail = getContentResolver().loadThumbnail(imageUri_t, thumbSize, null);

} catch (IOException e) {
e.printStackTrace();
}

} else {
albumArt = cursor1.getString(cursor1.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART));
}
}

audio.setData(data);
audio.setTitle(title);
audio.setAlbum(album);
audio.setArtist(artist);
audio.setAlbumId(albumId);
audio.setAlbumArt(albumArt);
audio.setThumbnail(thumbnail);
audioList.add(audio);
}
}
cursor.close();

setAdapter();
}
  • And a part of MyAdapter:
@Override
public void onBindViewHolder(@NonNull MainHolder holder, int position) {
Audio audio = arrayList.get(position);
holder.txtTitle.setText("Title: " + audio.getTitle());
holder.txtAlbum.setText("Album: " + audio.getAlbum());
holder.txtArtist.setText("Artist: " + audio.getArtist());

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
if (audio.getThumbnail() != null)
holder.img.setImageBitmap(audio.getThumbnail());
} else {
if (audio.getAlbumArt() != null) {
File imgFile = new File(audio.getAlbumArt());
if (imgFile.exists()) {
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
holder.img.setImageBitmap(myBitmap);
}
}
}
}

First query on MediaStore.Audio.Media.ARTIST about fetch audio info from MediaStore.

There is a difference About fetch thumbnail of Audio file in android 10 (Q) and below.
So query for lower than Android 10 is like follow:

String albumArt = cursor1.getString(cursor1.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART));

You can get album art from ALBUM_ART column in MediaStore.Audio.Albums.

Then for Android 10 like follow this:

Query on MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI where albumId equals _ID column from MediaStore.Audio.Albums.
And load thumbnail from Content Resolver by image uri...

I hope it is useful ;)

Receiving album art in android 11

You need to pass in the Uri of the track.

//This will get you the uri of the track, if you already have the track id
Uri trackUri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, trackId);
Bitmap bm = contentResolver.loadThumbnail(trackUri, new Size(512,512), null);

Set the bitmap to an ImageView

imageView.setImageBitmap(bm);

Get Album Cover Image

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;
}

How to display the album art for a particular song?

Use this code to get the album art for particular song.

ContentResolver res = getContentResolver();
Uri smusicUri = android.provider.MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI;
Cursor music =res.query(smusicUri,null,null, null, null);

music.moveToFirst();
int x=music.getColumnIndex(android.provider.MediaStore.Audio.Albums.ALBUM_ART);
String albumArt = music.getString(x);
Bitmap bm= BitmapFactory.decodeFile(albumArt);
ImageView image=(ImageView)findViewById(R.id.IValbumArt);


Related Topics



Leave a reply



Submit