java.lang.IllegalArgumentException: sortOrder not supported

advertisements

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)


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.