从MediaStore.Audio.Albums.ALBUM_ART显示专辑封面

时间:2012-06-14 10:31:52

标签: android android-imageview android-mediaplayer albumart android-cursorloader

我在LoaderManager中使用CursorLoader和Custom CursorAdapter。我已经实现了显示专辑和相关的艺术家,现在我想要显示封面。

这是我的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;
    }   
}

我尝试了以下但没有成功:

    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);

    }

MediaStore.Audio.Albums.ALBUM_ART返回的内容以及如何使用它来解决ImageView的问题?感谢。

1 个答案:

答案 0 :(得分:8)

我已经完成了ImageViews中的相册封面加载:

    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);

问题是列表非常慢。我想我应该缩小封面的分辨率来消耗更少的内存。