Search code examples
androidandroid-mediaplayermediastore

Android MediaStore - SQLiteException: No Such Column: Title


I am trying to get a list of songs to fill a recyclerview. I have every other tab sorted, but for all songs, It refuses to work. I am using MediaStore.Audio.Media.TITLE.

This is my stack trace:

java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:309)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Caused by: android.database.sqlite.SQLiteException: no such column: title (code 1): , while compiling: SELECT _id, title, artist, album FROM album_info ORDER BY title ASC
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:179)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
at android.content.ContentProviderProxy.query(ContentProviderNative.java:421)
at android.content.ContentResolver.query(ContentResolver.java:493)
at android.content.ContentResolver.query(ContentResolver.java:435)
at xyz.timmo.music.SongsFragment$GetAlbums.doInBackground(SongsFragment.java:88)
at xyz.timmo.music.SongsFragment$GetAlbums.doInBackground(SongsFragment.java:71)
at android.os.AsyncTask$2.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
at java.lang.Thread.run(Thread.java:818) 

My cursor method is as so:

Cursor cursor = context.getContentResolver().query(
    MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
    new String[]{
            //MediaStore.Audio.Media._ID,
            MediaStore.Audio.Albums._ID,
            MediaStore.Audio.Media.TITLE,
            MediaStore.Audio.Artists.ARTIST,
            MediaStore.Audio.Albums.ALBUM
            //MediaStore.Audio.Media.DURATION
    },
    null,
    null,
    MediaStore.Audio.Media.TITLE + " ASC"
);
if (cursor != null) {
    //count = cursor.getCount();
    cursor.moveToFirst();
    do {
        artArrayList.add(cursor.getString(
                cursor.getColumnIndex(MediaStore.Audio.Albums._ID)));
        songsArrayList.add(cursor.getString(
                cursor.getColumnIndex(MediaStore.Audio.Media.TITLE)));
        artistsArrayList.add(cursor.getString(
                cursor.getColumnIndex(MediaStore.Audio.Artists.ARTIST)));
        albumsArrayList.add(cursor.getString(
                cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM)));
     } while (cursor.moveToNext());
     cursor.close();
}

So what is the right string to fetch? All other topics suggest to use MediaStore.Audio.Media.TITLE but this seems to be missing. Is this something which was removed in the latest sdk?


Solution

  • I figured it out. I was fetching MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI in the cursor when I should be fetching MediaStore.Audio.Media.EXTERNAL_CONTENT_URI

    Here is the working code for future reference:

    Cursor cursor = context.getContentResolver().query(
            MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
            new String[]{
                    MediaStore.Audio.Media._ID,
                    //MediaStore.Audio.Albums._ID,
                    MediaStore.Audio.Media.TITLE,
                    MediaStore.Audio.Media.ARTIST,
                    MediaStore.Audio.Media.ALBUM
                    //MediaStore.Audio.Media.DURATION
            },
            null,
            null,
            MediaStore.Audio.Media.TITLE + " ASC"
    );
    if (cursor != null) {
        //count = cursor.getCount();
        cursor.moveToFirst();
        do {
            artArrayList.add(cursor.getString(
                    cursor.getColumnIndex(MediaStore.Audio.Albums._ID)));
            songsArrayList.add(cursor.getString(
                    cursor.getColumnIndex(MediaStore.Audio.Media.TITLE)));
            artistsArrayList.add(cursor.getString(
                    cursor.getColumnIndex(MediaStore.Audio.Artists.ARTIST)));
            albumsArrayList.add(cursor.getString(
                    cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM)));
        } while (cursor.moveToNext());
        cursor.close();