gpt4 book ai didi

Android:按专辑搜索轨道时 MediaStore 中的光标返回 0

转载 作者:行者123 更新时间:2023-11-29 01:26:06 26 4
gpt4 key购买 nike

我正在尝试按专辑 ID 查找当前保存在外部存储器中的轨道列表。获得有效的专辑 ID 后,只有当专辑有超过 1 首轨道时,我才能获取轨道列表。这有点奇怪,因为所有只有一首轨道的专辑都返回 0 光标大小,而应该至少有一个轨道。如果专辑中有超过 1 首轨道,则轨道列表已成功接收。这是代码 fragment ,

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());

最佳答案

您在运行 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());
}

关于Android:按专辑搜索轨道时 MediaStore 中的光标返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33979905/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com