I am able to query the Android MediaStore but I cannot setup the “where” statement. So I gave up on trying to get it and tried to hack around it and my hack also doesn’t work.
My hack is to move through each row of my cursor and see if the string value of the column I’m interested in is equivalent to my search keyword… My code is below. What I am trying to do is find all songs in an album (or artist, playlist).
Would appreciate any help setting up the Query for “where” or why my string comparison isn’t working…
private void loadList() {
String selection = ((HashMap<String, String>) selectedAlbumData.get(0))
.get("Album").toString();
String[] proj = { MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.ALBUM, MediaStore.Audio.Media.DURATION };
// managed query doesn't need startManagingCursor called on the
Cursor c = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
proj, null, null, null);
if (c.getCount() < 1) {
Log.d("AFA", "Cursor query is empty.. :( ...");
} else {
// do stuff with our content...
while (c.moveToNext()) {
HashMap<String, String> temp = new HashMap<String, String>();
if (c.getString(c.getColumnIndex(MediaStore.Audio.Media.ALBUM)) == selection) {
//do stuff with this data
}
Log.d("SongChoosing", "C str is: " + c.getString(c.getColumnIndex(MediaStore.Audio.Media.ALBUM)));
}
//debug stuff
c.moveToPosition(5);
Log.d("SongChoosing", "Selection is: " + selection);
Log.d("SongChoosing", "C str is: " + c.getString(c.getColumnIndex(MediaStore.Audio.Media.ALBUM)));
}
}
The string value I’m looking for in the albums column is a valid string, its not null or empty.. I tried printing every row of my songs on the log cat and the strings look equivalent to the naked eye but my if statement isn’t working…
Thanks a ton to anyone who can help me.
You have to use equals or equalsIgnoreCase instead of == with strings.
Try this