-1

My audio player on phone is showing "SQLite exception" and crashed. It needs external storage permission but I don't have a memory card. How to resolve this error?

Exception:

android.database.sqlite.SQLiteException: no such column: date_addedDESC (Sqlite code 1 SQLITE_ERROR): , while compiling: SELECT _id, _display_name, duration, _size, album_id FROM audio WHERE ((is_pending=0) AND (is_trashed=0) AND (volume_name IN ( 'external_primary' ))) AND (date_addedDESC), (OS error - 2:No such file or directory)

Main activity :

private void fetchPlayer() {
        //define a list to carry players
        List<player> mPlayer = new ArrayList<> ();
        Uri mediaStoreUri;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        mediaStoreUri = MediaStore.Audio.Media.getContentUri (MediaStore.VOLUME_EXTERNAL);
    }else{
        mediaStoreUri = MediaStore.Audio.Media. EXTERNAL_CONTENT_URI;
    }

        // define projection
        String[] projection = new String[]{
                MediaStore.Audio.Media._ID,
                MediaStore.Audio.Media.DISPLAY_NAME,
                MediaStore.Audio.Media.DURATION,
                MediaStore.Audio.Media.SIZE,
                MediaStore.Audio.Media.ALBUM_ID,
        };

        // order
         String sortOrder = MediaStore.Audio.Media.DATE_ADDED + " DESC  ";

        // get the players
        try (Cursor cursor = getContentResolver ().query (mediaStoreUri,projection,sortOrder,null,null)) {
            // cache cursor indices
            int idColumn = cursor.getColumnIndexOrThrow (MediaStore.Audio.Media._ID);
            int nameColumn = cursor.getColumnIndexOrThrow (MediaStore.Audio.Media.DISPLAY_NAME);
            int durationColumn = cursor.getColumnIndexOrThrow (MediaStore.Audio.Media.DURATION);
            int sizeColumn = cursor.getColumnIndexOrThrow (MediaStore.Audio.Media.SIZE);
            int albumColumn = cursor.getColumnIndexOrThrow (MediaStore.Audio.Media.ALBUM_ID);

            //clear the previous loaded before adding loading again
             while (cursor.moveToNext ()){
                //get the values of a column for a given audio file
                 long id = cursor.getLong(idColumn );
                 String name = cursor.getString (nameColumn);
                 int duration = cursor.getInt (durationColumn);
                 int size = cursor.getInt (sizeColumn);
                 long albumId = cursor.getLong (albumColumn);
                 // player Uri
                 Uri uri = ContentUris.withAppendedId (MediaStore.Audio.Media.EXTERNAL_CONTENT_URI , id );
                 // album artwork uri
                 Uri albumArtWorkUri = ContentUris.withAppendedId (Uri.parse("content:// media/external/audio/albumart") , albumId);
                 // remove mp3 extension from players name
                 name = name.substring (0 , name.lastIndexOf ("."));
                 //player item
                 player player  = new player (name , uri , albumArtWorkUri , size , duration , id );
                 //add player item to play list
                 mPlayer.add(player);
             }

             //display player
             showPlayers(mPlayer);

        }
    }

    private void showPlayers(List<player> mPlayer){

        if(mPlayer.size () == 0){
            Toast.makeText (this , "No Players" , Toast.LENGTH_SHORT ).show ();
            return;
        }

        // save players
        allPlayer.clear ();
        allPlayer.addAll (mPlayer);
        //update the tools bar title
        String title = getResources ().getString (R.string.app_name) + "." + mPlayer.size ();
        Objects.requireNonNull (getSupportActionBar ()).setTitle (title);
        //layout manager
        LinearLayoutManager layoutManager = new LinearLayoutManager (this);
        recyclerView.setLayoutManager (layoutManager);
        //players adapter
        playerAdapter = new playerAdapter (this , mPlayer);
        //set the adapter to recycleView
        recyclerView.setAdapter (playerAdapter);

    }
}
user4157124
  • 2,809
  • 13
  • 27
  • 42
  • 1
    Please elaborate about date_addedDESC. Where is it in you code? – blackapps Oct 24 '22 at 07:02
  • String sortOrder = MediaStore.Audio.Media.DATE_ADDED + " DESC "; its error about this code – Ahmed Abubaker Oct 24 '22 at 07:45
  • and when I add this line code like column showing other error private static final String COLUMN_DATE_ADDED = "DESC"; and pass this column as string column in to while (cursor.moveToNext ()){ show error in log : android.database.sqlite.SQLiteException: near "DESC": syntax error (Sqlite code 1 SQLITE_ERROR): – Ahmed Abubaker Oct 24 '22 at 07:48
  • Well please comment on the error first. What happened? You see that you did not program date_addedDESC but .. – blackapps Oct 24 '22 at 07:49
  • I do not understand your last comment. – blackapps Oct 24 '22 at 07:51
  • the error and exception occurs cause this line code like a column : String sortOrder = MediaStore.Audio.Media.DATE_ADDED + " DESC "; – Ahmed Abubaker Oct 24 '22 at 07:55
  • You programmed `MediaStore.Audio.Media.DATE_ADDED + " DESC "` which is equal to `date_added DESC` but in the error message is `date_addedDESC`. I had expected you to see this difference and had started your post telling and asking us about the missing space character. – blackapps Oct 24 '22 at 08:11
  • yes and when I delete "DESC" or write this small character for this string DESC show in logs: java.lang.StringIndexOutOfBoundsException: String index out of range: -1 in this line code name = name.substring (0 , name.lastIndexOf (".")); – Ahmed Abubaker Oct 24 '22 at 08:31
  • `this small character` ??? You mean a `space` character? – blackapps Oct 24 '22 at 08:57
  • yes sir and I'm share logs above now for it is exception – Ahmed Abubaker Oct 24 '22 at 22:59
  • and if I will press space as space character inside string column in String sortOrder = MediaStore.Audio.Media.DATE_ADDED + "(" +"_Desc"; like this or else – Ahmed Abubaker Oct 25 '22 at 13:05

2 Answers2

0

SortOrder is last parameter:

Cursor cursor = getContentResolver ().query (mediaStoreUri,projection,null,null,sortOrder)
D T
  • 3,522
  • 7
  • 45
  • 89
0

ok I solve it's problem I just space in sortOrder variable and I just click:

1- Build 2- then Clean Project 3- and Rebuild Project String sortOrder = MediaStore.Audio.Media.DATE_ADDED + " DESC ";

maybe to help any Developer