I'm trying to select multiple folders by querying the mediaStore. I can select a single folder by sing this piece of code:
String[] whereVal;
String selection;
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
selection = MediaStore.Audio.Media.DATA + " like ? " ;
whereVal = new String[]{"%Folder%"};
String[] projection = {MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.DURATION,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.ALBUM_ID};
Cursor cursor = getActivity().getContentResolver().query(uri, projection, selection, whereVal, null);
However when I try to select multiple folders mediaStore returns nothing.
Code:
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
selection = MediaStore.Audio.Media.DATA + " IN(?,?)" ;
whereVal = new String[]{"%Top 20 English%","%Audio%"};
String[] projection = {MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.DURATION,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.ALBUM_ID};
Cursor cursor = getActivity().getContentResolver().query(uri, projection, selection, whereVal, null);
Care to help me out? Thank in advance.
PS: I don't want to use OR operator.
Related
Some users told me, that my audio app doesn't find any audio files on their devices.
I use mediastore query and contentResolverto get the ID or path from the audio files.
I don't believe, that I have a problem in my code, so the question is, if mediastore or contentResolverdepends on devices?
Here is an example how I did searching for devices with Android lower Q:
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
projection = new String[]{
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.ALBUM,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.DURATION,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.TRACK,
MediaStore.Audio.Media.YEAR,
MediaStore.Audio.Media.DATE_ADDED
};
String sortOder = MediaStore.Audio.Media.TRACK + " ASC";
selection = MediaStore.Audio.Media.DATA + " like ?";
selectionArgs = new String[]{directory};
Cursor cur = getContentResolver().query(uri, projection, selection, selectionArgs, sortOder);
Can someone give me a hint or has had a similar experience?
On development devices this behavior doesn't happen, so that no log is available.
I am trying to get list of mp3 files from a particular folder.
As of now, I am using contentResolver.query for getting the music from my phone.
Like this:
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
String[] projection = {
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.DURATION
};
ContentResolver contentResolver = context.getContentResolver();
cursor = contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,projection, selection,null,null);
I am looking for solution to scan only a particular folder. How do I do this using contentResolver?
Thanks!
Finally figured it out myself. Hope it will help others.
Instead of this:
cursor = contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projection, selection, null, null);
I used this:
cursor = contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projection,MediaStore.Audio.Media.DATA + " like ? ",
new String[] {"%MyMusicFolder%"}, null);
i use
String[] projection = {MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.ALBUM,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.DURATION,
MediaStore.Audio.Media.YEAR,
MediaStore.Audio.Media.ALBUM_ID};
Cursor cursor;
cursor = сontext.getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
projection, selection, null, null);
and get data from tracks
while (cursor.moveToNext()) {
Log.e(TAG, "Track =" + cursor.getString(3));
Track track = new Track();
track.setLocalId(cursor.getInt(0));
track.setArtist(cursor.getString(1));
track.setAlbum(cursor.getString(2));
track.setTitle(cursor.getString(3));
track.setPath(cursor.getString(4));
track.setFileName(cursor.getString(5));
track.setLength(cursor.getLong(6));
track.setYear(cursor.getInt(7));
track.setAlbumId(cursor.getLong(8));
track.setGenre(context.getString(R.string.unknown_genre));
tracks.add(track);
}
cursor.close();
In the first case, on my real device HTC One V, in Log i see:
Track =Eye of the tiger(OST Рокки Бальбоа),
the second, on Genymotion emulator for example, in Log:
Track =Eye of the tiger(OST ?????????? ??????????????)
Please tell me how to solve this problem?
My application worked fine when I used the original Google Music Player. It found songs and playlists, no problem. Since I started using Play Music, my application can't find any of this. Here is my code:
Cursor cursor = contentResolver.query(uri, null, MediaStore.Audio.Media.IS_MUSIC + " = 1", null, null);
if (cursor == null || !cursor.moveToFirst()) {
Log.e(TAG, "Could not locate any music on device.");
return;
}
cursor.close();
Any Idea why this happens. I just got my first complaint that someon who purchased my application could not play music.
Maybe its late but i do something like this:
String[] projection = {
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.ALBUM_ID,
MediaStore.Audio.Media.DURATION
};
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
Cursor cursor = context.getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
projection,
selection,
null,
null);
In my Android program, I'm trying to use a managedQuery with a selection argument, but I get an Sql exception.
07-26 19:30:44.364: INFO/Database(1163):
sqlite returned: error code = 1, msg = near ".": syntax error
Here is my example code
String[] proj = { MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.ALBUM_ID,
MediaStore.Audio.Media.IS_MUSIC };
// Example without selection, works
String selection = null;
Cursor musiccursor = managedQuery(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, proj, selection, null,
null);
// Example with selection, does not work
selection = "MediaStore.Audio.Media._ID = 1";
musiccursor = managedQuery(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, proj, selection, null,
null);
Thanks, Niclas
The selection should be initialized like this:
selection = MediaStore.Audio.Media._ID+" = 1";
If you include the MediaStore.Audio.Media._ID in quotes the constant name is sent to SQLite to evaluate instead of actual column name _id.