如何从歌曲中分类专辑?

所以我的问题是,当我尝试对专辑进行排序时,专辑标题和专辑封面都是错误的。

我尝试对专辑ID进行排序,但这并不能解决问题,因为专辑ID与明显排序艺术无关。

当我遗漏排序时,一切都是正确的,但当我尝试对它们进行排序时,相册名称与专辑封面不匹配。

如何在片段中对我的专辑进行排序?

在这下面你可以找到我的代码。

提前致谢,

文斯

歌曲模型

// Columns I'll retrieve from the song table String[] columns = { SONG_ID, SONG_TITLE, SONG_ARTIST, SONG_ALBUM, SONG_ALBUMID, SONG_FILEPATH, }; // Limits results to only show music files. // // It's a SQL "WHERE" clause - it becomes `WHERE IS_MUSIC=1`. // final String musicsOnly = MediaStore.Audio.Media.IS_MUSIC + "=1"; // Querying the system cursor = resolver.query(musicUri, columns, musicsOnly, null, null); if (cursor != null && cursor.moveToFirst()) { do { // Creating a song from the values on the row Song song = new Song(cursor.getInt(cursor.getColumnIndex(SONG_ID)), cursor.getString(cursor.getColumnIndex(SONG_FILEPATH))); song.setTitle (cursor.getString(cursor.getColumnIndex(SONG_TITLE))); song.setArtist (cursor.getString(cursor.getColumnIndex(SONG_ARTIST))); song.setAlbumID (cursor.getInt(cursor.getColumnIndexOrThrow(SONG_ALBUMID))); song.setAlbum (cursor.getString(cursor.getColumnIndexOrThrow(SONG_ALBUM))); // Using the previously created genre and album maps // to fill the current song genre. String currentGenreID = songIdToGenreIdMap.get(Long.toString(song.getId())); String currentGenreName = genreIdToGenreNameMap.get(currentGenreID); song.setGenre(currentGenreName); // Adding the song to the global list songs.add(song); } while (cursor.moveToNext()); } else { // What do I do if I can't find any songs? } cursor.close(); public ArrayList getArtists() { ArrayList artists = new ArrayList(); for (Song song : songs) { String artist = song.getArtist(); if ((artist != null) && (! artists.contains(artist))) artists.add(artist); } // Making them alphabetically sorted Collections.sort(artists, new Comparator() { @Override public int compare(String o1, String o2) { return o1.compareTo(o2); } }); return artists; } /** * Returns an alphabetically sorted list with all the * albums of the scanned songs. * * @note This method might take a while depending on how * many songs you have. */ public ArrayList getAlbums() { ArrayList albums = new ArrayList(); for (Song song : songs) { String album = song.getAlbum(); if ((album != null) && (! albums.contains(album))) albums.add(album); } 

歌类

 public class Song implements Serializable { private long id; private String data; private String title = ""; private String artist = ""; private int albumid = -1; private String album = ""; private String genre = ""; public Song(long songId, String songData){ this.id = songId; this.data = songData; } public long getId(){ return id; } public String getData(){return data;} //Optional meta data public void setTitle(String title){ this.title = title; } public String getTitle() { return title; } public void setArtist(String artist){ this.artist = artist; } public String getArtist() { return artist; } public int getAlbumID() { return albumid; } public void setAlbumID(int albumid) { this.albumid = albumid; } public void setAlbum(String album){ this.album = album; } public String getAlbum() { return album; } public void setGenre(String genre) { this.genre = genre; } public String getGenre() { return genre; } } 

首先,我不确定为什么你在按歌曲存储你的返回值时试图按专辑排序(参见上面的@Usman Rafi),但是……

将全局arraylist添加到片段的顶部

 ArrayList 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在您查询的选择变量中…

这将返回唯一的专辑名称(它也只会返回专辑中的一首歌曲),如果在mediastore数据库中重复专辑(通过同一专辑中的多首歌曲),则只会添加与您的查询匹配的第一个实例到你的光标。

对专辑进行排序……

使用排序顺序对专辑返回的光标行进行排序; 我个人使用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(); 

您现在可以按照arraylist中的位置访问已排序的专辑信息…您可以使用我在顶部向您展示的Uri构建器获取专辑封面…

喜欢这个

 Song Album = Albums.get(position); imageView.setURI(ContentUris.withAppendedId(Uri.parse("content://media/external/audio/albumart"), Album.getAlbumID()); 

我希望这对你有用。

我仍然认为你应该建立一个名为Album的数据类

首先,你应该制作一个专辑 ,其中包含一个特定专辑的信息。 然后实现ComparableComparator Interface。

  • 如果相册始终基于相同字段进行排序,则可比较
  • 比较器,如果您需要实现多个排序逻辑

如果我正确理解您的代码,您将创建一个包含相册的字符串列表,然后独立于原始查询歌曲列表对该列表进行排序。 这意味着专辑封面艺术仍未受到影响。 我建议实施类比或比较。

2,coverart和title不匹配的原因是因为你排序了一个而忽略了另一个。 考虑以下示例:

排序前的专辑 –

  1. 世界
  2. 你好

排序前的ID-1 2

排序后的专辑1.你好2.世界

但身份仍然如此。 所以当你使用id列表为coverart构建uri时,在位置0选择的id是位置1的专辑。

在不改变代码的情况下,您可以通过使用比较器对歌曲进行排序来实现您的目的。 只要您需要使用其中一个属性(在您的情况下为QuerySongs对象的专辑名称)对对象进行排序,就可以使用比较器

getAlbumsId方法中,添加以下内容

 public ArrayList getAlbumsId() { ArrayList albumids = new ArrayList(); Collections.sort(songs, new Comparator() { @Override public int compare(QuerySongs o1, QuerySongs o2) { return o1.getAlbum().compareTo(o2.getAlbum()); } }); for (QuerySongs song : songs){ Long albumid = song.getAlbumID(); if (! albumids.contains(albumid)) { albumids.add(albumid); } } return albumids; } 

上面会改变songs对象,如果你不想发生这种情况,请复制它。 Crux是使用比较器对歌曲进行排序。