Reputation: 491
I'm trying to grab album information from MediaStore. I can get the name and artist very easily but for some reason the ALBUM_ID column is giving me an error when I try to access it.
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:450)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51 )
at android.database.CursorWrapper.getString(CursorWrapper.java:114)
at sage.musicplayer.MainActivity.getAlbumList(MainActivity.java:1540)
at sage.musicplayer.MainActivity.onCreate(MainActivity.java:238)
I can't seem to find a solution. Any help is appreciated! Below is the method I have to grab the album information and add them to an ArrayList.
public ArrayList<Album> getAlbumList() {
ArrayList<Album> temp = new ArrayList<>();
/*String[] proj = {MediaStore.Audio.Albums.ALBUM_ID,
MediaStore.Audio.Albums.ARTIST,
MediaStore.Audio.Albums.ALBUM,
MediaStore.Audio.Albums.NUMBER_OF_SONGS
};*/
Cursor albumCursor = getContentResolver().query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, null, null, null, null);
if(albumCursor != null && albumCursor.moveToFirst()) {
int albumName = albumCursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM);
int albumArtist = albumCursor.getColumnIndex(MediaStore.Audio.Albums.ARTIST);
int albumID = albumCursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ID);
do {
String thisAlbumName = albumCursor.getString(albumName);
String thisAlbumArtist = albumCursor.getString(albumArtist);
String thisAlbumID = albumCursor.getString(albumID);//this line is giving me an error
temp.add(new Album(thisAlbumName, thisAlbumArtist, thisAlbumID));
}while(albumCursor.moveToNext());
}
return temp;
}
Upvotes: 3
Views: 1097
Reputation: 4841
I too faced this issue. You can easily solve this issue by changing
int albumID = albumCursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ID);
TO
int albumID = albumCursor.getColumnIndex(MediaStore.Audio.Albums._ID);
That is you need to use _ID
whenever you want AlbumID
of the Album. I have no idea why MediaStore
developers used _ID
instead of ALBUM_ID
, etc stuff.
Upvotes: 2