Android: 按专辑搜索曲目时 MediaStore 中的光标 returns 0

Android: Cursor in MediaStore returns 0 while searching Tracks by Album

我正在尝试按专辑 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());

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