我想从专辑ID中获取歌曲和其他媒体详细信息。我只有专辑ID,我尝试了许多解决方案,但都没有成功。
我的代码片段:

ContentResolver contentResolver = getContentResolver();
        Uri mediaUri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, albumId);
        Log.wtf("SKJDBKJ", mediaUri.toString());
        Cursor mediaCursor = contentResolver.query(mediaUri, null, null, null, null);

        // if the cursor is null.
        if(mediaCursor != null && mediaCursor.moveToFirst())
        {
            Log.wtf("DSJK", "entered cursor");
            //get Columns
            int titleColumn = mediaCursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM);
            int idColumn = mediaCursor.getColumnIndex(MediaStore.Audio.Media._ID);
            int artistColumn = mediaCursor.getColumnIndex(MediaStore.Audio.Media.ARTIST);

            // Store the title, id and artist name in Song Array list.
            do
            {
                long thisId = mediaCursor.getLong(idColumn);
                String thisTitle = mediaCursor.getString(titleColumn);
                String thisArtist = mediaCursor.getString(artistColumn);
                Log.wtf("Title", thisTitle);

                // Add the info to our array.
                songArrayList.add(new Song(thisId, thisTitle, thisArtist));
            }
            while (mediaCursor.moveToNext());

            // For best practices, close the cursor after use.
            mediaCursor.close();
        }

Log formediaUri返回当前唱片集的路径,例如:content://media/external/audio/media/41
有人告诉我怎么做?

最佳答案

我自己做的。你可以在contentresolver.query上为'selection'字符串编写简单的sqlite查询。这个例子可以告诉你如何按唱片集ID获取歌曲。我认为这是最好的方法。

    ArrayList<Song> songs = new ArrayList<>();
    String selection = "is_music != 0";

    if (albumId > 0) {
        selection = selection + " and album_id = " + albumId;
    }

    String[] projection = {
            MediaStore.Audio.Media.TITLE,
            MediaStore.Audio.Media.ARTIST,
            MediaStore.Audio.Media.DATA,
            MediaStore.Audio.Media.DISPLAY_NAME,
            MediaStore.Audio.Media.DURATION,
            MediaStore.Audio.Media.ALBUM,
            MediaStore.Audio.Media.ALBUM_ID
    };
    final String sortOrder = MediaStore.Audio.AudioColumns.TITLE + " COLLATE LOCALIZED ASC";

    Cursor cursor = null;
    try {
        Uri uri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        cursor = getActivity().getContentResolver().query(uri, projection, selection, null, sortOrder);
        if (cursor != null) {
            cursor.moveToFirst();
            int position = 1;
            while (!cursor.isAfterLast()) {
                Song song = new Song();
                song.setTitle(cursor.getString(0));
                song.setDuration(cursor.getLong(4));
                song.setArtist(cursor.getString(1));
                song.setPath(cursor.getString(2));
                song.setPosition(position);
                song.setAlbumId(cursor.getLong(6));

                cursor.moveToNext();
            }
        }

    } catch (Exception e) {
        Log.e("Media", e.toString());
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }

08-18 05:21
查看更多