首先,我不确定当您按歌曲存储返回的值时为什么要尝试按专辑排序(请参阅上面的@Usman Rafi),但是..
将全局数组列表添加到片段的顶部
ArrayList<Song> Albums = new Arraylist<>();
不要尝试添加流派信息 - 您不需要它来实现您的目的
我尝试对专辑 ID 进行排序,但这并不能解决问题,因为专辑 ID 显然与对艺术进行排序无关。
专辑封面 Uri 可以写为:
ContentUris.withAppendedId(Uri.parse("content://media/external/audio/albumart"),
cursor.getInt(cursor.getInt(cursor.getColumnIndexOrThrow(SONG_ALBUMID))));
所以专辑封面和album_id实际上是有着千丝万缕的联系的。
所以我的问题是,当我尝试对专辑进行排序时......
在查询的选择变量中使用 MediaStore.Audio.Media.IS_MUSIC + "=1 ) GROUP BY (" + MediaStore.Audio.Media.ALBUM ...
这将返回唯一的专辑名称(它也只会返回专辑中的一首歌曲),如果专辑在您的媒体存储数据库中重复(通过在同一专辑中包含多首歌曲),则只会添加与您的查询匹配的第一个实例到你的光标。
对相册进行排序...
使用排序顺序对相册返回的光标行进行排序;我个人使用sql的字母顺序(符号、数字、a、b、c......)对它们进行排序
您应该注意这里排序是区分大小写的,除非您指定“COLLATE NOCASE”
要编写您的查询并对其进行排序,我将使用以下代码:
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);
之后,您可以简单地移动光标,将每一行添加到您构建的数据对象中,无需进一步排序,并且事情应该按正确的顺序排列。
我个人只是循环浏览
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();
您现在可以按数组列表中的位置访问排序后的专辑信息...您可以使用我在顶部向您展示的 Uri 构建器来访问专辑封面...
像这样
Song Album = Albums.get(position);
imageView.setURI(ContentUris.withAppendedId(Uri.parse("content://media/external/audio/albumart"),
Album.getAlbumID());
我希望这对你有用。
我仍然认为你应该构建一个名为 Album 的数据类