我正在尝试按专辑ID查找当前保存在外部存储器中的曲目列表。获得有效的专辑ID后,我可以仅在专辑有超过1首曲目的情况下获取曲目列表。这有点奇怪,所有专辑都有一条曲目返回0光标大小,其中应该至少有一个曲目。如果专辑有超过1个曲目,则曲目列表正在成功接收。这是代码片段,
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection = MediaStore.Audio.Media.ARTIST_ID + "=" + id;
Cursor c = context.getContentResolver().query(
uri,
null,
MediaStore.Audio.Media.IS_MUSIC + " != 0 AND " + selection,
null, MediaStore.Audio.Media.TITLE + " ASC");
ArrayList<MediaItem> listOfSongs = new ArrayList<MediaItem>();
c.moveToFirst();
while (c.moveToNext()) {
MediaItem songData = new MediaItem();
String title = c.getString(c.getColumnIndex(MediaStore.Audio.Media.TITLE));
String artist = c.getString(c.getColumnIndex(MediaStore.Audio.Media.ARTIST));
String album = c.getString(c.getColumnIndex(MediaStore.Audio.Media.ALBUM));
long duration = c.getLong(c.getColumnIndex(MediaStore.Audio.Media.DURATION));
String data = c.getString(c.getColumnIndex(MediaStore.Audio.Media.DATA));
long albumId = c.getLong(c.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
long artistId = c.getLong(c.getColumnIndex(MediaStore.Audio.Media.ARTIST_ID));
String composer = c.getString(c.getColumnIndex(MediaStore.Audio.Media.COMPOSER));
songData.setTitle(title);
songData.setAlbum(album);
songData.setArtist(artist);
songData.setDuration(duration);
songData.setPath(data);
songData.setAlbumId(albumId);
songData.setArtistId(artistId);
songData.setComposer(composer);
listOfSongs.add(songData);
}
c.close();
Log.d("SIZE", "SIZE: " + listOfSongs.size());
答案 0 :(得分:1)
在运行while循环之前,您已使用c.MoveToFirst()
权限。因此,当循环开始时,索引将移动到下一个项而不是第一个。检查我修改后的代码版本,
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection = "";
// album was requested
if (is_album) {
selection = MediaStore.Audio.Media.ALBUM_ID + "=" + id;
}
// artist was requested
else {
selection = MediaStore.Audio.Media.ARTIST_ID + "=" + id;
}
Cursor c = context.getContentResolver().query(
uri,
null,
MediaStore.Audio.Media.IS_MUSIC + " != 0 AND " + selection,
null, MediaStore.Audio.Media.TITLE + " ASC");
ArrayList<MediaItem> listOfSongs = new ArrayList<MediaItem>();
if (c == null) {
// error - log some message
}
else if (c.getCount() < 1) {
// nothing to show - log some message
}
else {
while (c.moveToNext()) {
MediaItem songData = new MediaItem();
String title = c.getString(c.getColumnIndex(MediaStore.Audio.Media.TITLE));
String artist = c.getString(c.getColumnIndex(MediaStore.Audio.Media.ARTIST));
String album = c.getString(c.getColumnIndex(MediaStore.Audio.Media.ALBUM));
long duration = c.getLong(c.getColumnIndex(MediaStore.Audio.Media.DURATION));
String data = c.getString(c.getColumnIndex(MediaStore.Audio.Media.DATA));
long albumId = c.getLong(c.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
long artistId = c.getLong(c.getColumnIndex(MediaStore.Audio.Media.ARTIST_ID));
String composer = c.getString(c.getColumnIndex(MediaStore.Audio.Media.COMPOSER));
songData.setTitle(title);
songData.setAlbum(album);
songData.setArtist(artist);
songData.setDuration(duration);
songData.setPath(data);
songData.setAlbumId(albumId);
songData.setArtistId(artistId);
songData.setComposer(composer);
listOfSongs.add(songData);
}
c.close();
Log.d("SIZE", "SIZE: " + listOfSongs.size());
}