Display album art from MediaStore.Audio.Albums.ALBUM_ART

Jecimi picture Jecimi · Jun 14, 2012 · Viewed 9.8k times · Source

I use a CursorLoader in a LoaderManager with a Custom CursorAdapter. I've already achieved to display the Album and the associated Artist, and now I'd like to display the Cover.

Here is my Custom CursorAdapter :

public class AlbumsAdapter extends CursorAdapter {


    private final LayoutInflater mInflater;

     public AlbumsAdapter(Context context, Cursor c) {
        super(context, c);
        mInflater=LayoutInflater.from(context);

    }
    @Override
    public void bindView(View view, Context context, Cursor cursor) {

        TextView albumTitle =(TextView)view.findViewById(R.id.albumTextView);
        albumTitle.setText(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM)));

        TextView artistName=(TextView)view.findViewById(R.id.artistTextView);
        artistName.setText(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Albums.ARTIST)));

        ImageView albumCover=(ImageView)view.findViewById(R.id.artistTextView);
        // Here what should I do ?

    }
    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        final View view=mInflater.inflate(R.layout.albums_row,parent,false); 
        return view;
    }   
}

I've tried the following without success :

    String path = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART));
    File imgFile = new  File(path);
    if(imgFile.exists()){

        Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

        ImageView albumCover=(ImageView)view.findViewById(R.id.album_cover);
        albumCover.setImageBitmap(myBitmap);

    }

What does MediaStore.Audio.Albums.ALBUM_ART returns and how to use it to ake the ImageView disolay the cover ? Thanks.

Answer

Jecimi picture Jecimi · Jun 15, 2012

I've achieved to load the Albums Covers in the ImageViews with :

    String coverPath = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART));
    Drawable img = Drawable.createFromPath(coverPath);
    ImageView coverAlbum=(ImageView)view.findViewById(R.id.album_cover);
    coverAlbum.setImageDrawable(img);

The problem is that the list is very slow. I guess that I should scale down the resolution of the cover to consume less memory.