如何显示专辑的封面?

时间:2017-01-23 17:21:13

标签: android android-glide

我正在使用此代码显示列表视图中歌曲的封面:

package com.xrobotx.love.musicalbums;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

public class SongAdapter extends BaseAdapter {

    private List<Song> songs;
    private LayoutInflater songInf;
    Context mContext;

    public SongAdapter(Context c, List<Song> theSongs){
        songs=theSongs;
        songInf=LayoutInflater.from(c);
        mContext = c;
    }

    @Override
    public int getCount() {
        return songs.size();
    }

    @Override
    public Object getItem(int arg0) {
        return songs.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        //map to song layout
        RelativeLayout songLay = (RelativeLayout)songInf.inflate
                (R.layout.song_layout, parent, false);

        //get title and artist views
        TextView songView = (TextView)songLay.findViewById(R.id.song_title);
        TextView artistView = (TextView)songLay.findViewById(R.id.song_artist);
        TextView durationView = (TextView)songLay.findViewById(R.id.song_duration);
        ImageView coverView = (ImageView)songLay.findViewById(R.id.song_cover);

        //get song using position
        Song currSong = songs.get(position);

        //get title and artist strings
        songView.setText(currSong.getTitle());
        artistView.setText(currSong.getArtist());
        durationView.setText(String.format("%02d:%02d",
                TimeUnit.MILLISECONDS.toMinutes((long) currSong.getDuration()),
                TimeUnit.MILLISECONDS.toSeconds((long) currSong.getDuration()) -
                        TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long) currSong.getDuration()))));


        if(currSong.getCover() != null){
            Glide.with(mContext).load(currSong.getCover()).override(50, 50).into(coverView);
        }


        songLay.setTag(position);
        return songLay;
    }

}

并非所有封面都能正确显示。有些封面是黑色的。 为什么?

编辑:

这是获取封面的代码:

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

1 个答案:

答案 0 :(得分:1)

我碰到了这个。像任何其他请求一样滑动网络请求有时会失败

所以我找到的解决方案是在失败时提出另一个请求。

 Glide.with(context)
.load((currSong.getCover())
.asBitmap()
.into(new SimpleTarget<Bitmap>(width, height) {
     @Override
     public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
         //handle Bitmap
         coverview.setBitmap(resource);
     }

     @Override
     public void onLoadFailed(Exception e, Drawable errorDrawable) {
         Glide.with(mContext).load(currSong.getCover()).override(50, 50).into(coverView);

     }
});