如何使用MediaStore查询来获取艺术家没有重复?如何使用、艺术家、MediaStore

2023-09-09 21:08:54 作者:么哒哒∩_∩

我想用 MediaStore ;我曾使整个列表,但所有的条目,由于多个环节的歌曲/艺术​​家重复。有没有什么办法可以使用​​查询来获得只能有一个,没有任何重复?

I would like to get all the artists names on my phone by using MediaStore ;Already I have the whole list but with duplicates of all entries due to multiple links songs/artists. Is there any way to use the query to get only one of each, without any duplicates?

还有就是我的查询:

    String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";

    String[] projection = {
            MediaStore.Audio.Media.ALBUM,
            MediaStore.Audio.Media.ARTIST,
            MediaStore.Audio.Media.TITLE,
            MediaStore.Audio.Media.DATA,
            MediaStore.Audio.Media.DURATION
    };
    Cursor cursor = context.getContentResolver().query(
            MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
            projection,
            selection,
            null,
            null);

在此先感谢!

Thanks in advance !

推荐答案

使用关键字DISTINCT的查询返回非重复值 例如,查询许多音频数字时只列出专辑名称

Using the keyword DISTINCT in the query returns non duplicate values For example, listing only album names when querying many audio numbers

        new String[] { "DISTINCT " + MediaStore.Audio.Playlists.Members.ALBUM_ID + " as _id"}

但我发现,你只能返回一列

but I found you can only return one column

我一直是简单,没有通过游标循环时,以下。传递的价值,你的情况艺术家,

I kept it simple and did the following when looping through the cursor. Pass the value, in your case the artist,

    if (value.length() > 0) {
        // check if value already exists
        if (!listItems.contains(value)) {
            // doesn't exist, add it
            listItems.add(value);
        }

    }

这是一个简单的方法。 你可以看看这个在我的应用程序的播放列表管理器在谷歌播放。我用这个方法来选择艺术家,年等。

This is a simple method. You can check this out in my app Playlist Manager on Google Play. I use this technique for selecting artists, years, etc.