So my problem is that when i try to sort the albums, the album title and album art are wrong.
I tried sorting the album ids but that doesn\'t fix it because album id hav
First, I'm not sure why you are trying to sort by album when you are storing your returned values by song (see @Usman Rafi above), but..
Add a global arraylist to the top of your fragment
ArrayList Albums = new Arraylist<>();
don't try to add genre information--you don't need it for your purpose
I tried sorting the album ids but that doesn't fix it because album id have nothing to do with sorting the art apparently.
Album art Uri's can be written as:
ContentUris.withAppendedId(Uri.parse("content://media/external/audio/albumart"),
cursor.getInt(cursor.getInt(cursor.getColumnIndexOrThrow(SONG_ALBUMID))));
So album art and album_id are actually inextricably linked.
So my problem is that when i try to sort the albums...
Use MediaStore.Audio.Media.IS_MUSIC + "=1 ) GROUP BY (" + MediaStore.Audio.Media.ALBUM in the selection variable of your query...
This will return unique album names (it will also only return one song within the album), if the album is repeated (by having several songs from the same album) in your mediastore database, only the first instance which matches your query will be added to your cursor.
to sort the albums...
Use the sort order to sort cursor rows which are returned by album; I personally sort them using sql's alphabetical order (symbols, numbers, a, b, c....)
You should note here that sorting is case sensitive unless you specify "COLLATE NOCASE"
to write your query and sort it I would use the following code:
String[] projection = {"DISTINCT " + MediaStore.Audio.Media.ALBUM_ID,
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.ALBUM,
MediaStore.Audio.Media.IS_MUSIC};
String selection = MediaStore.Audio.Media.IS_MUSIC +
"=1 ) GROUP BY (" + MediaStore.Audio.Media.ALBUM_ID;
String sort = MediaStore.Audio.Media.ALBUM + " COLLATE NOCASE ASC";
Cursor cursor = context.
getContentResolver().
query(MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI,
projection,
selection,
null,
sort);
After this you can simply move through your cursor adding each row to the data object you built, there is no need for further sorting, and things should be in the proper order.
I personally just loop through
if(cursor != null && cursor.getCount >0){
while(cursor.moveToNext()){
//create new song item and add the album information you need
Song album = new Song(cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media._ID)),
cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA)));
album.setAlbumId(cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID)));
album.setAlbumId(cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM)));
//add the Song item to the global arraylist
Albums.add(album)
}
}
cursor.close();
you can now access the sorted album info by position in the arraylist... you can get to the album art using the Uri builder i showed you at the top...
like this
Song Album = Albums.get(position);
imageView.setURI(ContentUris.withAppendedId(Uri.parse("content://media/external/audio/albumart"),
Album.getAlbumID());
I hope this is useful to you.
i still think you should build a data class called Album