So I've been trying this for some time now. To get the album art for an mp3 file and displaying it on its respective ImageView and I am using the uri("content://media/external/audio/albumart")
This is my method for getting the album art
public Bitmap getAlbumArt(long idAlbum){
Bitmap bitmap = null;
try{
final Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
Uri uri = ContentUris.withAppendedId(sArtworkUri, idAlbum);
ParcelFileDescriptor parcelFileDescriptor = getContext().getContentResolver().openFileDescriptor(uri,"r");
if (parcelFileDescriptor != null){
FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor);
}
}catch (Exception e){
e.printStackTrace();
}
return bitmap;
}
and this method always returns
java.io.FileNotFoundException: No entry for content://media/external/audio/albumart/31726
where the 31726 is the album id.
Since I'm catching this exception and I set it to a default Album art if it returns null, every mp3 has its ImageView set to the default album art. I am using my Samsung Galaxy s3 to run the application and my device runs android 4.2.2 JellyBean. Please someone help me getting this right.
This is how I request the album id
Uri musicUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String[] columns = {
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Albums._ID,
MediaStore.Audio.Media.DATA
};
String where = MediaStore.Audio.Media.IS_MUSIC + "=1";
Cursor musicCursor = usicResolver.query(musicUri,columns,where,null, null);
Then in an if loop with condition
if(musicCursor.moveToFirst()){
int albumId = musicCursor.getColumnIndex
(MediaStore.Audio.Albums._ID);
do{
long idAlbum = musicCursor.getLong(albumId);
//Then i send it to my above method getAlbumArt
Bitmap songAlbumArt = getAlbumArt(idAlbum);
}while(musicCursor.moveToNext());
}
This is how I query the cover art path from the album id:
private static String getCoverArtPath(long albumId, Context context) {
Cursor albumCursor = context.getContentResolver().query(
MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
new String[]{MediaStore.Audio.Albums.ALBUM_ART},
MediaStore.Audio.Albums._ID + " = ?",
new String[]{Long.toString(albumId)},
null
);
boolean queryResult = albumCursor.moveToFirst();
String result = null;
if (queryResult) {
result = albumCursor.getString(0);
}
albumCursor.close();
return result;
}
You can get the Bitmap from the BitmapFactory using the path returned from the method above:
BitmapFactory.decodeFile(coverArtPath);