1

I want to be able to get the album art of the song that is currently playing. I am able to get the name, album, artist with the use of

 private BroadcastReceiver mReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        String cmd = intent.getStringExtra("command");
        Log.v("tag ", action + " / " + cmd);
        String artist = intent.getStringExtra("artist");
        String album = intent.getStringExtra("album");
        String track = intent.getStringExtra("track");
        Log.v("tag", artist + ":" + album + ":" + track);
        Fullname = (artist + ":" + album + ":" + track);
        Toast.makeText(MusicPlayer.this, track, Toast.LENGTH_SHORT).show();
        update();

However, this does not help me with getting album art. Most of the posts on here that ask for getting album art call for the use of

MediaStore.Audio.AlbumColumns.AlbumArt

But I can't seem to figure out how to work it. When I tried to use

Cursor cursor = getActivity().managedQuery(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
}

(From here ) or anything similar to this I keep running into the problem of not having a albumId. I read that I should set albumid to

album_id = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID))

but I can't seem to get a Long without having another cursor which I can't figure out how to do to without the use of a pre-existing albumid. Any help would be greatly appreciated.

Community
  • 1
  • 1
Seabass77
  • 187
  • 5
  • 13

2 Answers2

0

I had the same problem. The intent only has the media id. So we have to get the album id related to that media file.

Here's the solution I used :

    //get the song's id from intent
    long songId = intent.getLongExtra("id", -1);

    //get the albumid using media/song id
    if(songId!=-1) {
            String selection = MediaStore.Audio.Media._ID + " = "+songId+"";

            Cursor cursor = getContentResolver().query(
                    MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, new String[] {
                            MediaStore.Audio.Media._ID, MediaStore.Audio.Media.ALBUM_ID},
                    selection, null, null);

            if (cursor.moveToFirst()) {
                long albumId = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
                Log.d("Album ID : ", ""+albumId);

                Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
                Uri albumArtUri = ContentUris.withAppendedId(sArtworkUri, albumId);

                //set the album art in imageview    
                albumArt.setImageURI(albumArtUri);
            }
            cursor.close();
    }
Ashish Tanna
  • 645
  • 1
  • 10
  • 25
0

Get Album ID of current playing song and call following function to get Album Art :

public static Bitmap getAlbumart(Context context,Long album_id){
    Bitmap bm = 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();
            bm = BitmapFactory.decodeFileDescriptor(fd, null, options);
            pfd = null;
            fd = null;
        }
    } catch(Error ee){}
    catch (Exception e) {}
    return bm;
}
Subhash Khimani
  • 427
  • 7
  • 22