简体   繁体   中英

How to fetch Cover Art for MEDIA (MediaStore.Audio.Media) in android (API 19)

So i'm trying to build a very simple music player (I started working with android just a few days ago). Till now I wrote the code to retrieve songs informations. However I'm not able to retrieve the album art for every single media. I know there is a column ALBUM_ART in MediaStore.Audio.Album (I tried to retrieve the Album art for each album and it worked), but I want to assign the album art to each song and I don't know how to do it. Surfing the net I found this kind of code:

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

But it doesn't seem to work, all I get is null . Can someone explain me how to retrieve the album art for each song?

String[] columns = { android.provider.MediaStore.Audio.Albums._ID, android.provider. MediaStore.Audio.Albums.ALBUM,android.provider.MediaStore.Audio.Albums.ALBUM_ART }; int album_column_index=0; Cursor cursor = getActivity().managedQuery( MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, columns, null, null, null);

        if (cursor.moveToFirst()) {
            do {


                arrAlbumNAme.add( cursor.getString(cursor
                        .getColumnIndex(android.provider.MediaStore.Audio.Albums.ALBUM)));
                album_column_index = cursor.getColumnIndexOrThrow(android.provider.MediaStore.Audio.Albums._ID);
                //cursor.moveToPosition(position);
                albumID = cursor.getInt(album_column_index);
                Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
                Uri uri = ContentUris.withAppendedId(sArtworkUri, albumID);
                arrAlbumArt.add(cursor.getString(cursor
                        .getColumnIndex(android.provider.MediaStore.Audio.Albums.ALBUM_ART)));

            } while (cursor.moveToNext());
        }

Inside adapter holder.image_icon.setImageUri(arrAlbumArt.get(position));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM