Question

I want to retrieve data from MediaStore especially the number of albums per artist but SQLite throws me an exception

no such column: number_of_albums (code 1): while compiling: SELECT artist_key, artist, number_of_albums FROM audio WHERE (is_music != 0)

Can someone explain me what I am doing wrong?

My code :

private List<Artist> getArtistList() {
    String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";

    String[] projection = {
            MediaStore.Audio.Artists.ARTIST_KEY,
            MediaStore.Audio.Artists.ARTIST,
            MediaStore.Audio.Artists.NUMBER_OF_ALBUMS
    };

    Cursor cursor = this.getActivity().getApplicationContext().getContentResolver().query(
            MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
            projection,
            selection,
            null,
            null);

    List<Artist> artists = new ArrayList<Artist>();
    Artist tmp = null;

    if (cursor.moveToFirst()) {
        do {    
            tmp = new Artist(cursor.getString(0),
                    cursor.getString(1),
                    Integer.parseInt(cursor.getString(2)));
            artists.add(tmp);
        } while (cursor.moveToNext());
    }

    return artists;
}

Thanks !

Was it helpful?

Solution

You're querying the wrong content URI for that column. Use MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI instead of MediaStore.Audio.Media.EXTERNAL_CONTENT_URI.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top