我试图获得所有歌曲专辑封面,但它显示以下运行时错误

时间:2017-09-11 09:12:04

标签: android audio-player albumart android-music-player

 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:无法从CursorWindow读取第0行col -1。在从中访问数据之前,请确保Cursor已正确初始化。

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) 

1 个答案:

答案 0 :(得分:0)

此前发布了一个有关此问题的问题。问题在于:确保Cursor在从中访问数据之前已正确初始化。

在此链接上发布了一个有用的答案: Android cursor error - "make sure cursor is initialized correctly before accessing data from it..."