Reputation: 30814
I'm interested in performing a query for all of the albums from a particular genre. Android makes it easy to perform a similar query for the albums from a particular artist by using MediaStore.Audio.Artists.Albums.getContentUri
, but as far as querying genres goes, MediaStore.Audio.Genres.Members.getContentUri
is the best provided way, but this is used to return the songs from a genre.
You can easily return the album names for each member of a genre after performing the Genres.Members
query and then using MediaStore.Audio.Genres.Members.ALBUM
, to return album names, but again this is useful only if you're showing the songs.
Is there some sort of selection I could make to return only the albums from a genre, or perhaps a special Uri
I could use? I haven't found any docs on this, so if anyone has any information it would greatly appreciated.
Upvotes: 1
Views: 1631
Reputation: 1808
Here you are:
{
int genreId = 2; // << Set Your Genre ID Here
Uri uri = MediaStore.Audio.Albums.getContentUri("external");
Log.i("XXX", "uri = " + uri.toString());
Cursor cursor = null;
try {
String selection = "album_info._id IN "
+ "(SELECT (audio_meta.album_id) album_id FROM audio_meta, audio_genres_map "
+ "WHERE audio_genres_map.audio_id=audio_meta._id AND audio_genres_map.genre_id=?)";
String[] selectionArgs = new String[] { String.valueOf(genreId) };
String[] proj = { AlbumColumns.ALBUM };
cursor = getContentResolver().query(uri, proj, selection,
selectionArgs, null);
if (null != cursor) {
Log.i("XXX", "cursor rows count = " + cursor.getCount());
while (cursor.moveToNext()) {
Log.i("XXX", " Album: " + cursor.getString(0));
}
cursor.close();
Log.i("XXX", "cursor closed");
}
} catch (Exception ex) {
Log.e("XXX", "Error Querying Database");
} finally {
if (cursor != null) {
cursor.close();
}
}
}
Edit
Query fixed to get all albums with specified genre Id
Upvotes: 7