I'm trying to show album art with album names.
If I use MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
then the album names are repeated; but I am able to show album art.
And if I use MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI
then album names are not repeated but no album art is shown.
How can I get unrepeated Album Names with album art (if available)?
Note: I created a songs list before in which song name, artist name, song duration and album arts were fetched. But I can't fetch album name and album art(either album art is fetched or album name) with same methodology in Albums tab (I have four tabs for Songs, Artists, Albums and Playlists).
Thank you for your help.
Here is the code:
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();
}
}