1

I've been searching in this page how to get the covert art from a mp3 file.

I'm developing one music app and I want to get the cover art of the song that is inside the mp3 file (ID3v2 tag). But, I have search a lot but I haven't found how can I do it.

Somebody know how to do it?

Thanks everyone.

Serj Moya
  • 149
  • 3
  • 10

1 Answers1

6

Here is my implementation of how I get the cover art. First I select an audio file:

MediaMetadataRetriever myRetriever = new MediaMetadataRetriever();
Uri selectedAudio;
//...       

//on button click or any other event
Intent intent = new Intent();
String chooser = "Select audio file";

intent.setType("audio/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, chooser), GET_AUDIO_CODE);

Then in onActivityResult i get the URI of the file:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == Activity.RESULT_OK)
    {
            selectedAudio = data.getData();
            MediaMetadataRetriever myRetriever = new MediaMetadataRetriever();
            myRetriever.setDataSource(this, selectedAudio); // the URI of audio file
            setArtwork(myRetriever);
    }
    //...
}

And after that I set the cover art:

//....

public boolean setArtwork(MediaMetadataRetriever myRetriever)
{
    byte[] artwork;

    artwork = myRetriever.getEmbeddedPicture();

    if (artwork != null)
    {
        Bitmap bMap = BitmapFactory.decodeByteArray(artwork, 0, artwork.length);
        ivArtwork.setImageBitmap(bMap);

        return true;
    }
    else
    {
        ivArtwork.setImageBitmap(null);

        return false;
    }
}
yyunikov
  • 5,719
  • 2
  • 43
  • 78
  • It works perfectly, thank you so much!!! I was a lot of days looking something to do it and I couldn't get a solution. That's incredible, thank you so much. – Serj Moya Aug 21 '13 at 11:49
  • And one more thing, do you know if whit MediaDataRetriever I can get the lyrics of the mp3file or I need an external library to do it. – Serj Moya Aug 21 '13 at 11:50
  • Glad that it helps :) I have never tried to get the lyrics. Go to android developers web-site and read about MediaDataRetriever. Good luck! – yyunikov Aug 21 '13 at 11:51
  • The last question, do you know something about this error: `dalvikvm(3186): adjustAdaptiveCoef max=4194304, min=1048576, ut=568` – Serj Moya Aug 21 '13 at 11:54
  • It's just a Dalvik messages, I don't know what this one about, but why do you need this? – yyunikov Aug 21 '13 at 12:01
  • Nothing special, only to know why this happen. – Serj Moya Aug 21 '13 at 12:07
  • It's just dalvik virtual machine message, nothing special :) Would be glad if you can +1 this post. Thank you. – yyunikov Aug 21 '13 at 12:08
  • I would like to do +1 in this post but I'm new and a I need a 15 reputation, so I don't have it right now. When I have 15 reputation I will do it. I promise you. – Serj Moya Aug 22 '13 at 06:11