2

I've been trying to have the thumbnail of the audio file in my songslist. But after several attempts I failed to do this. I do not know how to implement this. Is there any easy way to get the thumbnail of the audio file ? I have given the sample picture below that I am actually looking for.

private class MediaCursorAdapter extends SimpleCursorAdapter {

    MediaCursorAdapter(Context context, int layout, Cursor c) {
        super(context, layout, c, new String[]{MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.TITLE,
                         MediaStore.Audio.Media.ALBUM_ID},
                new int[]{R.id.title, R.id.displayname, R.id.thumbnail});
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        TextView title = view.findViewById(R.id.title);
        TextView name = view.findViewById(R.id.displayname);
        ImageView thumbnail = view.findViewById(R.id.thumbnail);

        MediaMetadataRetriever mmr = new MediaMetadataRetriever();
        byte[] rawArt;
        Bitmap art = null;
        BitmapFactory.Options bfo=new BitmapFactory.Options();

        mmr.setDataSource(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID)));
        rawArt = mmr.getEmbeddedPicture();

        if (null != rawArt)
            art = BitmapFactory.decodeByteArray(rawArt, 0, rawArt.length, bfo);

        thumbnail.setImageBitmap(art);

        name.setText(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE)));

        title.setText(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST)));


        view.setTag(cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DATA)));

    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.song_list, parent, false);

        bindView(v, context, cursor);

        return v;
    }
}

Audio thumbnail

Amin
  • 25
  • 1
  • 7
  • Possible duplicate of [Get cover picture by song](https://stackoverflow.com/questions/15314617/get-cover-picture-by-song) – Lucem Aug 07 '18 at 18:43
  • But this is not working for me. I am trying to display song thumbnail beside the title. but it did not display after I've tried the link you commented. – Amin Aug 07 '18 at 22:01
  • can you add some codes to the question(what you have tried so far). – karthik Aug 08 '18 at 03:37
  • I've updated with some codes to the the question that I had tried so far. Can you please help me to display the thumbnail of the audio file? – Amin Aug 12 '18 at 10:29
  • Possible duplicate of [How can I display Album Art using MediaStore.Audio.Albums.ALBUM\_ART?](https://stackoverflow.com/questions/17573972/how-can-i-display-album-art-using-mediastore-audio-albums-album-art) – Yoav Feuerstein Dec 06 '18 at 12:37

3 Answers3

0

This is how you get the thumbnail of an audio file. if a null value is returned, it means that the audio doesn't have an image. You can as well set the default thumbnail icon for the audio or use a resource in your drawable as default.

Lucem
  • 2,912
  • 3
  • 20
  • 33
0

You should check this post out and see if it helps How can I display Album Art using MediaStore.Audio.Albums.ALBUM_ART? and is it confirmed that your custom listview adapter is working correctly

0

Credit goes to [Ashish Pedhadiya] (https://stackoverflow.com/questions/23265502/getting-album-art-for-music-player-android#:~:text=9-,Try%20following%3A,-MediaMetadataRetriever%20mmr%20%3D%20new)

we provide file absolute to MidiaMetaDataRetriver and get a bitmap from it.

MediaMetadataRetriever mmr = new MediaMetadataRetriever();
mmr.setDataSource(file.getPath());
byte [] data = mmr.getEmbeddedPicture();
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
imageView.setImageBitmap(bitmap);
Abhi S
  • 250
  • 2
  • 18