Serj Moya
Serj Moya

Reputation: 149

Get embedded cover art from a mp3 file

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.

Upvotes: 1

Views: 3748

Answers (1)

yyunikov
yyunikov

Reputation: 5907

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

Upvotes: 6

Related Questions