Suppose I have an Android block of code that looks something like this:
String[] proj = {MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media._ID};
int[] to = new int[] { R.id.artist_name };
Cursor musiccursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, proj, null, null, MediaStore.Audio.Media.ARTIST);
ListView musiclist = (ListView) findViewById(R.id.mylist);
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(getApplicationContext(), R.layout.songitem, musiccursor, proj, to);
musiclist.setAdapter(mAdapter);
But what I want, is this:
String selection = MediaStore.Audio.Media.FILE_PATH + " ilike '%audio%books%'";
String[] proj = {MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media._ID};
int[] to = new int[] { R.id.artist_name };
Cursor musiccursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, proj, selection, null, MediaStore.Audio.Media.ARTIST);
ListView musiclist = (ListView) findViewById(R.id.mylist);
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(getApplicationContext(), R.layout.songitem, musiccursor, proj, to);
musiclist.setAdapter(mAdapter);
The only problem, of course, is that FILE_PATH is not actually a column I can use, and as far as I can tell, no such column exists.
So I’m wondering:
- Is there a way to query only for music in a certain directory? If so, how?
- If that’s not an option, should I make a ListAdapter that filters by directory? If so, again, how would I go about in doing that?
Thanks for any advice.
OK, after many iterations of trying, I finally have an example that works and I thought I’d share it. My example queries the images MediaStore, then obtains the thumbnail for each image to display in a view. A few tweaks should provide you with the code needed to query the audio MediaStore instead. I am loading my images into a Gallery object, but that is not a requirement for this code to work:
Make sure you have a Cursor and int for the column index defined at the class level so that the Gallery’s ImageAdapter has access to them:
First, obtain a cursor of image IDs located in the folder:
Then, in the ImageAdapter for the Gallery, obtain the thumbnail to display:
I guess the most important section of this code is the managedQuery that demonstrates how to use MediaStore queries to filter a list of image files in a specific folder.