Home » Android » android – How can I display Album Art using MediaStore.Audio.Albums.ALBUM_ART?

android – How can I display Album Art using MediaStore.Audio.Albums.ALBUM_ART?

Posted by: admin May 14, 2020 Leave a comment

Questions:

I’m trying to build a MP3 player and I want the ImageView to display the album art of respective songs. I’ve tried the following, but it doesn’t work.

albumcover = (ImageView) findViewById(R.id.cover);

String coverPath = songsList.get(songIndex).get(MediaStore.Audio.Albums.ALBUM_ART);
Drawable img = Drawable.createFromPath(coverPath);
albumcover.setImageDrawable(img);

When I try to play the songs, all I get is a blank screen in the ImageView.

How to&Answers:

Here’s how I get album art for a song:

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

if (cursor.moveToFirst()) {
    String path = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART));
    // do whatever you need to do
}

albumId refers to MediaStore.Audio.Media.ALBUM_ID for that song.

If you’re are looking for album art for a particular song (rather than in a list of albums), as far as I know it’s a two-stage process since ALBUM_ART is a property of MediaStore.Audio.Albums and is not available directly as song metadata.

Answer:

If you have album ID you get Album Image uri :-

final public static Uri sArtworkUri = Uri
            .parse("content://media/external/audio/albumart");      

Uri uri = ContentUris.withAppendedId(PlayerConstants.sArtworkUri,
                listOfAlbums.get(position).getAlbumID());

And if you have a Image uri you can use any of the image loader Glide, Picaso, UIL to display images .

                               **OR**

you can write your own image loader

public Bitmap getAlbumart(Context context, Long album_id) {
    Bitmap albumArtBitMap = null;
    BitmapFactory.Options options = new BitmapFactory.Options();
    try {

        final Uri sArtworkUri = Uri
                .parse("content://media/external/audio/albumart");

        Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id);

        ParcelFileDescriptor pfd = context.getContentResolver()
                .openFileDescriptor(uri, "r");

        if (pfd != null) {
            FileDescriptor fd = pfd.getFileDescriptor();
            albumArtBitMap = BitmapFactory.decodeFileDescriptor(fd, null,
                    options);
            pfd = null;
            fd = null;
        }
    } catch (Error ee) {
    } catch (Exception e) {
    }

    if (null != albumArtBitMap) {
        return albumArtBitMap;
    }
    return getDefaultAlbumArtEfficiently(context.getResources());
}



public Bitmap getDefaultAlbumArtEfficiently(Resources resource) {

    if (defaultBitmapArt == null) {

        defaultBitmapArt = decodeSampledBitmapFromResource(resource,
                R.drawable.default_album_art, UtilFunctions
                        .getUtilFunctions().dpToPixels(85, resource),
                UtilFunctions.getUtilFunctions().dpToPixels(85, resource));

    }
    return defaultBitmapArt;
}

Answer:

 ContentResolver musicResolve = getContentResolver();
    Uri smusicUri = android.provider.MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI;
    Cursor music =musicResolve.query(smusicUri,null         //should use where clause(_ID==albumid)
        ,null, null, null);



    music.moveToFirst();            //i put only one song in my external storage to keep things simple
    int x=music.getColumnIndex(android.provider.MediaStore.Audio.Albums.ALBUM_ART);
        String thisArt = music.getString(x);


     Bitmap bm= BitmapFactory.decodeFile(thisArt);
        ImageView image=(ImageView)findViewById(R.id.image);
        image.setImageBitmap(bm);

Answer:

This method return ArrayList with song path and album art.

public static ArrayList<CommonModel> getAllMusicPathList(Context context) {
    ArrayList<CommonModel> musicPathArrList = new ArrayList<>();
    Uri songUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;

    Cursor cursorAudio = context.getContentResolver().query(songUri, null, null, null, null);
    if (cursorAudio != null && cursorAudio.moveToFirst()) {

        Cursor cursorAlbum;
        if (cursorAudio != null && cursorAudio.moveToFirst()) {

            do {
                Long albumId = Long.valueOf(cursorAudio.getString(cursorAudio.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID)));
                cursorAlbum = context.getContentResolver().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);

                    if(cursorAlbum != null && cursorAlbum.moveToFirst()){
                        String albumCoverPath = cursorAlbum.getString(cursorAlbum.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART));
                        String data = cursorAudio.getString(cursorAudio.getColumnIndex(MediaStore.Audio.Media.DATA));
                        musicPathArrList.add(new CommonModel(data,albumCoverPath ,false));
                    }

                 } while (cursorAudio.moveToNext());
        }
    }
    return musicPathArrList;
}

this is CommonModel .

public class CommonModel {

private String path;
private boolean selected;

public String getAlbumCoverPath() {
    return albumCoverPath;
}

public void setAlbumCoverPath(String albumCoverPath) {
    this.albumCoverPath = albumCoverPath;
}

private String albumCoverPath;

public CommonModel(String path, String albumCoverPath, boolean b) {
    this.path = path;
    this.albumCoverPath=albumCoverPath;
    this.selected=b;
}

public String getPath() {
    return path;
}

public void setPath(String path) {
    this.path = path;
}

public boolean getSelected() {
    return selected;
}

public void setSelected(boolean selected) {
    selected = selected;
}
}

Answer:

You should use Uri.parse(“content://media/external/audio/albumart”); to query the albumart. the 1st answer may get exception on some phone (at least mine)

Answer:

The below code worked for me. I know it has been answered already, it may be useful for someone checking for reference.

 public void getAlbumArt() {
    try {
        ContentResolver cr = getContentResolver();
        Uri uri = MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI;

        Cursor cursor = cr.query(uri, null, null, null, null);

        if (cursor != null && cursor.moveToFirst()) {
            int albumart = cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART);
            do {
                String Albumids = cursor.getString(albumart);
                Albumid.add(Albumids);

            } while (cursor.moveToNext());
        }cursor.close();
    } catch (NumberFormatException e){
        e.printStackTrace();
    }
}
public void getSelectedPath(){
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            String path= String.valueOf(listView.getItemAtPosition(i));

                if(path.equals("null")){
                ImageView imgv=(ImageView)view.findViewById(R.id.imageView);
                imgv.setImageResource(R.drawable.unknowalbum);
                imgv.setMaxHeight(50);
                imgv.setMaxWidth(50);

            }
            else{
                Drawable image=Drawable.createFromPath(path);
                ImageView imgview=(ImageView)view.findViewById(R.id.imageView);
                imgview.setImageDrawable(image);

            }
        }
    });
}

for complete code visit http://vasistaguru.blogspot.com/2017/02/get-albumart-and-trackname-using.html

Answer:

Example Code

public static Uri getAlbumArtUri(long albumId) {
    return ContentUris.withAppendedId(Uri.parse("content://media/external/audio/albumart"), albumId);
}


ArrayList<Uri> albumArtUris = new ArrayList<>();
Cursor cursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, new String[]{"album_id"}, null, null, null);
cursor.moveToFirst();
do {
    long ablumId = cursor.getLong(cursor.getColumnIndexOrThrow("album_id"));
    albumArtUris.add(getAlbumArtUri(ablumId));
} while (cursor.moveToNext());