1

The following code throws the exception in the title on the third and fourth line. Am I missing something? I want to be able to sort by artist name.

  public List<String> getAllArtists(Context context) {

       List<String> artists = new ArrayList<String>();
       String[] projection2 = {MediaStore.Audio.Media.ARTIST};
       String sortOrder = MediaStore.Audio.Artists.ARTIST;
       Uri songUri = Uri.parse("content://com.google.android.music.MusicContent/audio");
       CursorLoader cl2 = new CursorLoader(context,
                songUri, projection2, null, null, sortOrder);

       cursor = cl2.loadInBackground();

             while (cursor.moveToNext()) {
               if (cursor.getString(0).length()>0){
                if (!artists.contains(cursor.getString(0)))
                 artists.add(cursor.getString(0));
                }}

            cursor.close();

return artists;}

Here is the complete stack trace:

03-22 16:49:18.237 2594-2637/php_request E/AndroidRuntime: FATAL EXCEPTION: IntentService[SongService] Process: php_request, PID: 2594 java.lang.IllegalArgumentException: sortOrder not supported at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:165) at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135) at android.content.ContentProviderProxy.query(ContentProviderNative.java:421) at android.content.ContentResolver.query(ContentResolver.java:500) at android.content.CursorLoader.loadInBackground(CursorLoader.java:64) at SongParser.getAllArtists(SongParser.java:41) at SongService.onHandleIntent(SongService.java:60) at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:66) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.os.HandlerThread.run(HandlerThread.java:61)

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
Taylor Courtney
  • 813
  • 1
  • 6
  • 23

3 Answers3

3

What you're using is a Uri handled specifically by Google Play Music app, not Android media system. (com.google.android.music is package name of Play Music.) I can imagine that

a) Play Music API cannot be mixed with Android MediaStore API,

b) Play Music content provider does not support sorting in SQL as the error suggests,

c) Play Music API is changing over time so don't use it anyway. (Don't quote me on this.)

Maybe you'd like to use one of Uri constants specified here http://developer.android.com/reference/android/provider/MediaStore.Audio.Media.html. These are:

MediaStore.Audio.Media.INTERNAL_CONTENT_URI
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI

If using specified Uri is intended then I suggest removing sortOrder from content provider request and sorting the resulting List afterwards by calling

Collections.sort(artists);

as described here https://stackoverflow.com/a/708708/2444099.

Community
  • 1
  • 1
Eugen Pechanec
  • 37,669
  • 7
  • 103
  • 124
1

Seems like perhaps sort order API has changed. Can you sort on ARTIST_KEY? I'm just looking at columns on http://developer.android.com/reference/android/provider/MediaStore.Audio.AudioColumns.html and that one says it's sortable. Also does your query work when you have null sort order?

Willcodeforfun
  • 361
  • 3
  • 10
0

For your case, you can use it for sorting,

String sortOrder = MediaStore.Audio.Albums.DEFAULT_SORT_ORDER;

For URI, you can use-

Uri uri = MediaStore.Audio.Artists.Albums.getContentUri("external", artistId);

The artistId is the value of the MediaStore.Audio.Artists._ID from a previous Artist query.

Here, DEFAULT_SORT_ORDER: The default sort order for this table Constant Value: "artist_key"

Hope it will help you.


What CursorLoader does?

CursorLoader do sort for you when it is created as CursorLoader(Context context, Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)

So, to query for a list of artist, you would use MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI and use the artist name as your selection args. For a list of albums, you now have the ARTIST_KEY which you can use to query MediaStore.Audio.Artists.Albums.EXTERNAL_CONTENT_URI, to obtain a list of albums for the given artist.

Resource Link: Media store default sort order


Sort order in Android list from Mediastore

For Video, you can use it

String orderBy = android.provider.MediaStore.Video.Media.DATE_TAKEN;
     videocursor = managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
            proj, null, null, orderBy + " DESC");

Resource Link: sort order in android list from mediastore


Query Sorting using column name.

For specifying the sort order for the query to a content provider, it look like

new CursorLoader(context, RepresentativeEntityContentProvider.CONTENT_URI, null, null,
            null, "COLUMN_NAME ASC");

Using this we can make it ascending order with COLUMN_NAME.


Android SQLite Sort order does not work with upper and lowercase letters

If you are using SQLite database, with a Cursor or Perhaps Content provider, you may have come across alphabetical sorting problems regarding upper and lowercase letters.

String[] projection = { Table.COLUMN_ID, Table.COLUMN_TITLE};

String sortOrder = Table.COLUMN_TITLE + " COLLATE NOCASE ASC";

CursorLoader cursorLoader = new CursorLoader(this,  
    YourProvider.CONTENT_URI,
    projection,
    null,
    null,
    sortOrder);

The solution to the alphabetical sorting problem is the COLLATE NOCASE in the part of the query where you specify the sorting.

Note that the ASC is optional to specify. You could also use DESC to reverse the sorting order.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
SkyWalker
  • 28,384
  • 14
  • 74
  • 132