Я пытаюсь получить все обложки альбомов песен, но появляется следующая ошибка времени выполнения.

 public void getSongList() {
    //retrieve song info
    ContentResolver musicResolver = getContentResolver();
    Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null);

    if(musicCursor!=null && musicCursor.moveToFirst()){
        //get columns
        int titleColumn = musicCursor.getColumnIndex
                (android.provider.MediaStore.Audio.Media.TITLE);
        int idColumn = musicCursor.getColumnIndex
                (android.provider.MediaStore.Audio.Media._ID);
        int artistColumn = musicCursor.getColumnIndex
                (android.provider.MediaStore.Audio.Media.ARTIST);
        int dataColumn = musicCursor.getColumnIndex
                (android.provider.MediaStore.Audio.Media.DATA);
        int artColumn=musicCursor.getColumnIndex
                (android.provider.MediaStore.Audio.Albums.ALBUM_ART);
        //add songs to list
        do {
            long thisId = musicCursor.getLong(idColumn);
            String thisTitle = musicCursor.getString(titleColumn);
            String thisArtist = musicCursor.getString(artistColumn);
            String thisData = musicCursor.getString(dataColumn);

                String thisArt = musicCursor.getString(artColumn);
                File imgFile=new File(thisArt);
                if(imgFile.exists()){
                    albumArt= BitmapFactory.decodeFile(imgFile.getAbsolutePath());
                }


            songList.add(new Song(thisId, thisTitle, thisArtist,thisData,albumArt));
        }
        while (musicCursor.moveToNext());
        musicCursor.close();
    }
}

ФАТАЛЬНОЕ ИСКЛЮЧЕНИЕ: главная

    Process: com.example.kishan.experimentonmusicplayer, PID: 3373


java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kishan.experimentonmusicplayer/com.example.kishan.experimentonmusicplayer.MainActivity}: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
                                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2584)
                                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2666)
                                                                                          at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1493)
                                                                                          at android.os.Handler.dispatchMessage(Handler.java:111)
                                                                                          at android.os.Looper.loop(Looper.java:207)
                                                                                          at android.app.ActivityThread.main(ActivityThread.java:5769)
                                                                                          at java.lang.reflect.Method.invoke(Native Method)
                                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
                                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)

Причина: java.lang.IllegalStateException: не удалось прочитать строку 0, столбец -1 из CursorWindow. Убедитесь, что курсор правильно инициализирован, прежде чем обращаться к его данным.

at android.database.CursorWindow.nativeGetString(Native Method)
                                                                                      at android.database.CursorWindow.getString(CursorWindow.java:438)
                                                                                      at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:66)
                                                                                      at android.database.CursorWrapper.getString(CursorWrapper.java:137)
                                                                                      at com.example.kishan.experimentonmusicplayer.MainActivity.getSongList(MainActivity.java:161)
                                                                                      at com.example.kishan.experimentonmusicplayer.MainActivity.onCreate(MainActivity.java:81)
                                                                                      at android.app.Activity.performCreate(Activity.java:6583)
                                                                                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1114)
                                                                                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2531)
                                                                                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2666) 
                                                                                      at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1493) 
                                                                                      at android.os.Handler.dispatchMessage(Handler.java:111) 
                                                                                      at android.os.Looper.loop(Looper.java:207) 
                                                                                      at android.app.ActivityThread.main(ActivityThread.java:5769) 
                                                                                      at java.lang.reflect.Method.invoke(Native Method) 
                                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) 
                                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679) 

person Kishan Gupta    schedule 11.09.2017    source источник


Ответы (1)


Ранее был опубликован вопрос по этой проблеме. Проблема заключается в следующем: убедитесь, что курсор правильно инициализирован, прежде чем обращаться к его данным.

По этой ссылке был опубликован полезный ответ: Ошибка курсора Android - убедитесь, что курсор правильно инициализирован, прежде чем обращаться к его данным...

person Aleksandar Zoric    schedule 11.09.2017