开发者

Query the android Mediastore for Artist names of Music files

开发者 https://www.devze.com 2023-01-09 11:57 出处:网络
I\'m torn about how to implement this because Content Provider URI querys do not support the simple SQL \"DISTINCT\" query method to return a cursor to the Artists of the songs in the mediastore, remo

I'm torn about how to implement this because Content Provider URI querys do not support the simple SQL "DISTINCT" query method to return a cursor to the Artists of the songs in the mediastore, removing any duplicate entries.

I can query and get a cursor to all the artists, I'm just torn as 开发者_如何学运维to how to remove the dupes, or simply not show them.

I've tried using matrixcursor to construct a new cursor with the dupe entries removed, but it's slow (build an array of artists, if in that array don't copy to the new matrixcursor, etc.)

Can anyone recommend a better solution, or point me in the proper direction??

I've also thought about pre-loading the information i need into an array of objects - I'm just concerned with memory overhead in my application.

Thank You for any help you may provide.


The easiest way to get a list of all the artist (and albums is the same method) is to use the MediaStore.Audio.Artist for the quest. For Example something like this would get and show all the artist:

String[] proj = {MediaStore.Audio.Artists._ID, MediaStore.Audio.Artists.ARTIST, MediaStore.Audio.Artists.NUMBER_OF_ALBUMS, MediaStore.Audio.Artists.NUMBER_OF_TRACKS };
musiccursor = managedQuery(MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI, proj, null, null, MediaStore.Audio.Artists.ARTIST + " ASC");
String[] from= new String[]{ MediaStore.Audio.Artists.ARTIST, MediaStore.Audio.Artists.NUMBER_OF_ALBUMS, MediaStore.Audio.Artists.NUMBER_OF_TRACKS };
int[] to = new int[] { R.id.songname, R.id.rowlength, R.id.rowartist };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.musicrow, musiccursor, from, to);
SongsView.setAdapter(adapter);

Where SongsView would be your list to display them in. Everything else is using a simple Cursor adapter

Hope this helps


You might want to try creating stateful CursorWrapper, overriding the appropriate methods. Simply ensure every call to next() iterates through the cursor until it finds an artist name you consider appropriately unique, optionally storing seen artists in an ArrayList instance variable.


            ArrayList<SongBeen> genresList=new ArrayList<SongBeen>();

            String value=search+ "%";
            String[] inValue=new String[] {value};

                ContentResolver musicResolver = activity.getContentResolver();
                /*Uri musicInUri = android.provider.MediaStore.Audio.Genres.INTERNAL_CONTENT_URI;
                Cursor mInternalCursor = musicResolver.query(musicInUri, null, null, null, null);
            */  
                Uri musicExUri = android.provider.MediaStore.Audio.Genres.EXTERNAL_CONTENT_URI;
                Cursor mExternalCursor = musicResolver.query(musicExUri, null, MediaStore.Audio.Genres.NAME+ " like ?",
                        inValue, "LOWER(" + MediaStore.Audio.Genres.NAME + ") ASC");

                 Cursor[] cursors = {mExternalCursor};
                final MergeCursor mMergeCursor = new MergeCursor(cursors);
                if (mMergeCursor.moveToFirst()) {
                    do { 
                        SongBeen been=new SongBeen();
                        long thisId=       mMergeCursor.getLong(mMergeCursor.getColumnIndexOrThrow(MediaStore.Audio.Genres._ID));
                        String thisTrack = mMergeCursor.getString(mMergeCursor.getColumnIndexOrThrow(MediaStore.Audio.Genres.NAME));
                        been.setId(thisId);
                        been.setTrack(thisTrack);
                        genresList.add(been);

                    } while (mMergeCursor.moveToNext());
                } 

                mMergeCursor.close();

            return genresList;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号