【问题标题】:I am trying to get all the songs album art but it is showing the following runtime error我正在尝试获取所有歌曲专辑封面,但它显示以下运行时错误
【发布时间】:2017-09-11 09:12:03
【问题描述】:
 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。确保在从光标访问数据之前正确初始化光标。

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) 

【问题讨论】:

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


    【解决方案1】:

    之前发布了一个关于此问题的问题。问题出在此处:确保在从光标访问数据之前正确初始化光标。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多