尝试检索专辑封面时出现java.lang.IllegalStateException

时间:2016-02-16 02:42:48

标签: java android

ContentResolver musicResolver = getContentResolver();
Uri musicUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null);
String coverPath = musicCursor.getString(musicCursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART));

Throws IllegalStateException..
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.prasadam.smartcast/com.prasadam.smartcast.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:2421)
                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2481)
                                                                            at android.app.ActivityThread.access$900(ActivityThread.java:153)
                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                            at android.os.Looper.loop(Looper.java:148)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5432)
                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:735)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
                                                                         Caused by: 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.database.CursorWindow.nativeGetString(Native Method)
                                                                            at android.database.CursorWindow.getString(CursorWindow.java:438)
                                                                            at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
                                                                            at android.database.CursorWrapper.getString(CursorWrapper.java:137)
                                                                            at com.prasadam.smartcast.MainActivity.getSongList(MainActivity.java:169)
                                                                            at com.prasadam.smartcast.MainActivity.onCreate(MainActivity.java:60)
                                                                            at android.app.Activity.performCreate(Activity.java:6303)
                                                                            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
                                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2374)
                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2481) 
                                                                            at android.app.ActivityThread.access$900(ActivityThread.java:153) 
                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349) 
                                                                            at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                            at android.os.Looper.loop(Looper.java:148) 
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5432) 
                                                                            at java.lang.reflect.Method.invoke(Native Method) 
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:735) 
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625) 

1 个答案:

答案 0 :(得分:1)

尝试替换

Uri musicUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;

Uri musicUri = MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI;

希望它有所帮助。