Neno0o
Neno0o

Reputation: 133

Getting album art from the audio file Uri

I am trying to get the album art from the audio file Uri, here is my code:

// uri is the audio file uri

public static Bitmap getSongCoverArt(Context context, Uri uri){
    Bitmap songCoverArt = null;
    String[] projections = {MediaStore.Audio.Media.ALBUM_ID};
    Cursor cursor = null;
    try {
        cursor = context.getContentResolver().query(uri, projections, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID);
        cursor.moveToFirst();

        Uri songCover = Uri.parse("content://media/external/audio/albumart");
        Uri uriSongCover = ContentUris.withAppendedId(songCover, column_index);
        Log.d(TAG, uriSongCover.toString());
        ContentResolver res = context.getContentResolver();
        try {
            InputStream in = res.openInputStream(uriSongCover);
            songCoverArt = BitmapFactory.decodeStream(in);
        }catch (FileNotFoundException e){
            Log.e(TAG, e.getMessage());
        }
    }finally {
        if(cursor != null){
            cursor.close();
        }
    }

    return songCoverArt;
}

This function always returns "No entry for content://media/external/audio/albumart/0"

Upvotes: 3

Views: 5105

Answers (1)

Theo
Theo

Reputation: 2042

I think your problem lies in the appendId

   Uri songCover = Uri.parse("content://media/external/audio/albumart");
    Uri uriSongCover = ContentUris.withAppendedId(songCover, column_index)

replace column_index with Long _id of album rather than a column index which for _id is 0.

 album_id = c.getLong(c.getColumnIndex(MediaStore.Audio.Albums._ID));

where c is my cursor

Upvotes: 4

Related Questions