fatfreddyscat
fatfreddyscat

Reputation: 835

Filter a managedQuery by file extension (or, alternatively, file type) for an Android Cursor

I want to do a managedQuery using Android SDK where the results returned are filtered by their respective file extension (e.g. not the name, necessarily). I've done quite a bit of researching and am tired and hoping the community can help me.. I'm sure the answer is out there but, in lieu of reading an SQL book or whatever, I'm just trying to do something that should be pretty simple but not finding the solution.

What I want to do is, essentially, something like:

managedQuery(Audio.Media.EXTERNAL_CONTENT_URI, myProjection, Audio.Media.DATA + " like ? ", new String[] {"%mp3%"}, null );

This works EXCEPT that, if the title is something like "mymp3.wav", this query will still return it since 'mp3' is in the name but, that isn't what I want -> I want it filtered purely based on file extension (or, alternatively, file type if there is a way to do that [e.g. if an mp3's file extension name was changed to .wav even though the content of the file is actually mp3, that would work for my purposes as well]). The solution should indicate a way to filter for multiple extensions/file types (i.e. if I want the query to return all .mp3 files as well as .wav files but not .amr files, for example).

Furthermore, while I have you, I want to sort the results alphabetically. I have tried something like:

managedQuery(Audio.Media.EXTERNAL_CONTENT_URI, myProjection, null, null, MediaStore.Audio.Media.TITLE + " ASC" );

However, that doesn't filter the entire results... that is to say that, the list returned is sorted by, say, mp3 title and then, at the end, it then appends to the list the .wav files also sorted alphabetically but I want the entire list to be alphabetical regardless of file type.

I know someone on here can answer this easily -> thanks in advance!!!

Upvotes: 3

Views: 2163

Answers (1)

Kurtis Nusbaum
Kurtis Nusbaum

Reputation: 30825

I think all you have to do is change %mp3% to %mp3. This will force "mp3" to be at the end of the filename.

If you need to select multiple file types, do it like this:

managedQuery(
  Audio.Media.EXTERNAL_CONTENT_URI,
  myProjection, 
  Audio.Media.DATA + " like ? OR " + Audio.Media.DATA + " like ? ", 
  new String[] {"%mp3","%wav"}, 
  MediaStore.Audio.Media.TITLE + " ASC" );

Upvotes: 8

Related Questions