无法同时获取专辑封面和专辑名称

时间:2018-09-22 09:22:37

标签: android android-mediaplayer android-contentresolver mediastore albumart

我正在尝试使用专辑名称显示专辑封面。

如果我使用MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,则专辑名称会重复;但我可以展示专辑封面。

如果我使用MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,则不会重复专辑名称,但不会显示专辑封面。

如何获得带有专辑封面的重复的专辑名称(如果有)?

Screenshot

注意:我创建了一个歌曲列表,然后在其中提取了歌曲名称,艺术家名称,歌曲时长和专辑封面。但是我无法以相同的方法在“专辑”选项卡中获取专辑名称和专辑封面(获取了专辑封面或专辑名称)(我对“歌曲”,“艺术家”,“专辑”和“播放列表”有四个标签)。

谢谢您的帮助。

代码如下:

Albums.java

List<Album> sampleAlbum = new ArrayList<>();    
public void getAlbum() {
    ContentResolver contentResolver = getActivity().getContentResolver();
    Uri songUri = MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI;
    Cursor songCursor = contentResolver.query(songUri, null, null, null, null);

    if (songCursor != null && songCursor.moveToFirst()) {
        int songAlbum = songCursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM);
        int songPath = songCursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART);
        do {
            Album newAlbum = new Album();
            newAlbum.albumName = songCursor.getString(songAlbum);
            newAlbum.pathName = songCursor.getString(songPath);
            sampleAlbum.add(newAlbum);
        }

        while (songCursor.moveToNext());
    }
}

AlbumRecyclerAdapter.java

public class AlbumRecyclerAdapter extends RecyclerView.Adapter<AlbumViewHolder> {

private List<Album> albums;
private Context context;

public AlbumRecyclerAdapter(Context context, List<Album> albums) {
    this.albums = albums;
    this.context = context;
}

@Override
public AlbumViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.album_recycler_item, parent, false);
    return new AlbumViewHolder(view);
}

@Override
public void onBindViewHolder(AlbumViewHolder holder, final int position) {

    final Album sampleAlbum = albums.get(position);
    holder.album_name.setText(sampleAlbum.albumName);

    try {
        MediaMetadataRetriever mmr = new MediaMetadataRetriever();
        mmr.setDataSource(albums.get(position).pathName);

        byte[] data = mmr.getEmbeddedPicture();

        if (data != null) {
            Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
            holder.album_image.setImageBitmap(bitmap);
            holder.album_image.setAdjustViewBounds(true);
            holder.album_image.setLayoutParams(new LinearLayout.LayoutParams(500, 500));
        }
        else {
            holder.album_image.setImageResource(R.drawable.default);
            holder.album_image.setAdjustViewBounds(true);
            holder.album_image.setLayoutParams(new LinearLayout.LayoutParams(500, 500));
        }
    }
    catch (Exception e) {
        e.printStackTrace();
    }

    holder.constraintLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(context, "Album " + sampleAlbum.albumName + "'s songs will be opened", Toast.LENGTH_LONG).show();
        }
    });

}

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

0 个答案:

没有答案