Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query MediaStore Cursor using specific folder

I use a cursor to return items for a media based Listview. I would like to only return items that are in a specific folder.

This works for my Song list, as this cursor is based off MediaStore.Audio.Media.DATA, and I can check this for the folder in the cursor:

   final String folder = "'" + (new MyPrefs(this.PREF_PATH)).getString("music_folder",null) +  "%'";

   audioCursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, mCursorCols, 
            MediaStore.Audio.Media.DATA + " LIKE " + folder, null,MediaColumns.TITLE + " COLLATE LOCALIZED ASC");
   startManagingCursor(audioCursor);

However, in my Listview for artists, the DATA result is not available to check against:

audioCursor = getContentResolver().query(MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI, cols, 
            null, null,AudioColumns.ARTIST + " COLLATE LOCALIZED ASC");
startManagingCursor(audioCursor);    

Can anyone please share with me a good method for querying the MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI and only returning my specific folder? I need to use the Artist URI because I use the NUMBER_OF_TRACKS column, which is not available in the regular Media URI.

like image 253
Josh Avatar asked Nov 01 '11 10:11

Josh


1 Answers

You can cram whatever you want into your selection, like this sub-select for instance - which should solve your problem:

// The folder needs to end with the % wildcard or stuff doesn't work properly.
String folder = "/mnt/sdcard/Music/Some Folder/";
folder = folder + "%";
String where = Artists._ID + " IN (SELECT " + Media.ARTIST_ID + " FROM audio WHERE " +
    Media.DATA + " LIKE ?)";
String[] whereArgs = new String[]{folder};
audioCursor = getContentResolver().query(MediaStore.Audio.Artist.EXTERNAL_CONTENT_URI,
    cols, where, whereArgs, AudioColumns.ARTIST + " COLLATE LOCALIZED ASC");
like image 63
Jens Avatar answered Oct 30 '22 16:10

Jens