java 如何解决“没有这样的栏目:日期_添加的DESC”错误?

nc1teljy  于 2022-11-20  发布在  Java
关注(0)|答案(2)|浏览(127)

我的音频播放器在手机上显示“SQLite异常”和崩溃。它需要外部存储权限,但我没有存储卡。如何解决这个错误?
例外情况:
android.database.sqlite.SQLiteException:无此列:添加日期DESC(Sqlite代码1 SQLITE错误):,编译时:SELECT _id,_显示名称,持续时间,_大小,专辑标识FROM音频WHERE((is_pending=0)AND(is_trashed=0)AND(volume_name IN('external_primary')))AND(date_addedDESC),(操作系统错误- 2:没有这样的文件或目录)
主要活动:

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);

    }
}
uplii1fm

uplii1fm1#

SortOrder是最后一个参数:

Cursor cursor = getContentResolver ().query (mediaStoreUri,projection,null,null,sortOrder)
xoefb8l8

xoefb8l82#

好了,我解决了这个问题,我只是在sortOrder变量中输入space,然后我点击:
1-Build 2-然后Clean Project 3-和Rebuild Project字符串排序顺序=媒体存储.音频.媒体.添加日期+“DESC“;
也许可以帮助任何开发人员

相关问题