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

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

10-13 04:08