I want to display a list of song present in my internal storage along with the album art. I am using this code to load the songs in my list.
private void getSongsList() {
final Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
final String[] cursor_cols = { MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.ALBUM,
MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.ALBUM_ID,
MediaStore.Audio.Media.DURATION };
final String where = MediaStore.Audio.Media.IS_MUSIC + "=1";
final Cursor cursor = getActivity().getContentResolver().query(uri,
cursor_cols, where, null, null);
if(cursor != null && cursor.moveToFirst()) {
do {
String artist = cursor.getString(cursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
String album = cursor.getString(cursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));
String songName = cursor.getString(cursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
String data = cursor.getString(cursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
long albumId = cursor.getLong(cursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID));
int duration = cursor.getInt(cursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));
Uri sArtworkUri = Uri
.parse("content://media/external/audio/albumart");
Uri albumArtUri = ContentUris.withAppendedId(sArtworkUri, albumId);
songsList.add(new SongData(songName, artist, data, album, albumArtUri, duration));
}while (cursor.moveToNext());
cursor.close();
Collections.sort(songsList, new Comparator<SongData>(){
public int compare(SongData a, SongData b){
return a.getSongName().compareTo(b.getSongName());
}
});
}
}
I am using Glide to load the image to my image view using this
SongData sd = list.get(position);
Glide.with(context)
.load(sd.getAlbumArtUri())
.apply(new RequestOptions()
.placeholder(R.drawable.ic_music))
.into(holder.albumArt);
The issue it that all my songs are getting displayed in my RecyclerView but not with the album art rather with my place holder image. Not a single song had the album art. To ensure that my songs have a album art associated with it i have used another music player app and there my songs album art are getting displayed whats wrong with my code?
After a lot of trials and errors i finally made it work. The error with the above code was as below
java.lang.SecurityException(External path: /storage/emulated/0/Android/data/com.android.providers.media/albumthumbs/1525023642395: Neither user 10108 nor current process has android.permission.WRITE_EXTERNAL_STORAGE.)
Uri was resolving content://media/external/audio/albumart
to the external directory /storage/emulated/0/Android/data/com.android.providers.media/albumthumbs/
directory and writing or caching the album art from data base to device storage but it doesn't had any write permission.
So simply grant write storage permission and it will work
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Also request for the permission for API >= 23. You can also go and verify the album arts being cached in your internal storage directory
Android/data/com.android.providers.media/albumthumbs/