Glide + Recyclerview和MediaStore.Audio.Media.ALBUM_ID的图片错误

时间:2019-04-24 11:57:03

标签: java android android-recyclerview picasso android-glide

我正在使用此方法加载图像并将其保存到arraylist:

public ArrayList<Song> getMusic(){
    ArrayList<Song> list = new ArrayList<>();
    Cursor cursor = resolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
            null,
            null,
            null,
            MediaStore.MediaColumns.TITLE+" ASC");
    if(cursor!=null && cursor.moveToFirst()){
        int id = cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID);
        int title = cursor.getColumnIndex(MediaStore.Audio.Media.TITLE);
        int artist = cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST);
        int album = cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM);
        do{
            Song song = new Song(cursor.getString(id),
                    cursor.getString(title),
                    cursor.getString(artist),
                    cursor.getString(album));
            list.add(song);
        }while(cursor.moveToNext());
        cursor.close();
    }
    return list;
}

然后我像这样将图像加载到recyclerView中:

Uri uri = Uri.withAppendedPath(Uri.parse("content://media/external/audio/albumart"), song.getId());
        Glide.with(itemView)
                .load(uri)
                .into(thumb);

我得到这个结果:

https://i.stack.imgur.com/TGoog.jpg

问题是箭头所在的位置有一个图像,该图像不应存在。该图像是从我的相册加载的。

我已经找到了使用Picasso的解决方案,其中的代码和recyclerView中的URI相同:

Picasso.get().load(uri)
                .into(this.thumb);

但是,当然,图像是完全尺寸的,并且当我滚动视图时有时会显得有些滞后。结果是这样的:

https://i.stack.imgur.com/fmtTZ.jpg

如您所见,第二张图像已按我的意愿完美加载,但是当我添加调整大小的图像“ Picasso.resize(sizeX,sizeY)”时,该图像看起来像我发布的第一张图像。 / p>

所以我想知道是否有任何方法可以使用Glide加载专辑图像,因为就我而言,对应用程序进行性能分析在内存管理中会更好。

编辑(这是歌曲适配器):

public class SongAdapter extends RecyclerView.Adapter<SongAdapter.ViewHolder> implements SectionTitleProvider {

private ArrayList<Song> list;
private onItemClickListener listener;
private int type; // 0 for list, 1 for grid

public SongAdapter(ArrayList<Song> list, int type, onItemClickListener listener){
    this.list = list;
    this.listener = listener;
    this.type = type;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view;
    if(type==0){
        view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.music_item,null,false);
    }else{
        view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.music_item_grid,null,false);
    }
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
    viewHolder.setData(i, listener);
}

@Override
public int getItemCount() {
    return list.size();
}

@Override
public String getSectionTitle(int position) {
    return list.get(position).getTitle().substring(0,1);
}

public class ViewHolder extends RecyclerView.ViewHolder {

    private TextView music, artist;
    private ImageView thumb;
    private ImageButton more;

    public ViewHolder(@NonNull View itemView) {
        super(itemView);
        music = itemView.findViewById(R.id.textMusic);
        artist = itemView.findViewById(R.id.textArtist);
        thumb = itemView.findViewById(R.id.musicThumb);
        more = itemView.findViewById(R.id.musicOption);
    }

    public void setData(int i, final onItemClickListener listener){
        final Song song = list.get(i);
        music.setText(song.getTitle());
        artist.setText(song.getArtist());

        /*
        Glide.with(itemView)
                .load(uri)
                .into(thumb);
                */

        Uri uri = Uri.withAppendedPath(Uri.parse("content://media/external/audio/albumart"), song.getId());
        Picasso.get().load(uri)
                .into(this.thumb);

        more.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(final View v) {
                 final PopupMenu popup = new PopupMenu(v.getContext(), more);
                 popup.inflate(R.menu.song_menu);
                 popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                     @Override
                     public boolean onMenuItemClick(MenuItem item) {
                         switch (item.getItemId()){
                             case R.id.music_menu_playnow:
                                 return true;
                             case R.id.music_menu_addqueue:
                                 return true;
                             case R.id.music_menu_addplaylist:
                                 return true;
                             case R.id.music_menu_share:
                                 return true;
                             default:
                                 return onMenuItemClick(item);
                         }
                     }
                 });

                 popup.show();
             }
         });
         itemView.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 listener.onItemClick(song);
             }
         });
    }
}

public interface onItemClickListener{
    void onItemClick(Song song);
}

}

0 个答案:

没有答案