Moses
Moses

Reputation: 433

Is it possible to get bitmap(Album art) from mp3 file or file inputstream?

Is it possible to retrieve bitmap( Album art ) of mp3 file from it's file path or file input stream. Like using BitmapFactory API or some other API.

Upvotes: 3

Views: 4481

Answers (3)

Tanay Mondal
Tanay Mondal

Reputation: 147

Yes, it is possible to get mp3 cover art.

MediaMetadataRetriever mmr = new MediaMetadataRetriever();
mmr.setDataSource(mPath);
InputStream inputStream = null;
if (mmr.getEmbeddedPicture() != null) {
    inputStream = new ByteArrayInputStream(mmr.getEmbeddedPicture());
 }
mmr.release();

bitmap = BitmapFactory.decodeStream(inputStream);

Upvotes: 5

dishooom
dishooom

Reputation: 498

To extract album art from an mp3 file, there is no direct API available in Android as of now. You need to extract it via FFmpeg for the same.

Upvotes: 0

Kartheek
Kartheek

Reputation: 7214

Using the following method you can get the album art uri of an image file.

public Uri getArtUriFromMusicFile(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.getContentResolver().query(uri, cursor_cols, where, null, null);
        Log.d(TAG, "Cursor count:" + cursor.getCount());
        /*
         * 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;
    }

Upvotes: 4

Related Questions