MediaStore to list from specific path - android

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);

Related

Android ContentResolver can't find any audio files on some devices

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.

Android Q - Query MediaStore for audioFiles in specific folder and its subfolders

I'm trying to query Android Q mediaStore using contentResolver. I want to get all audiofiles from a specific Folder with all its subdirectories (for example /sdcard/Download/AudioFiles/Folder 1 and /sdcard/Download/AudioFiles/Folder 2/Folder 3).
Perrmission android.permission.READ_EXTERNAL_STORAGE is set in my manifest.xml and also runtime permissions are used.
I did this in Android Studio Emulator, thats why I use download folder
When I select folder Downloads in fileChooser to get all audiofiles in the subdirecories I got no results (Cursor.getCount() = 0).
When I select the directory /sdcard/Download/AudioFiles/Folder 2 I got the expected results.
This behaviour comes from DocumentFile.getName()which gives me the last part of the filesystem.
The fileChosser selection Downloads gives me download. Please not the plurals "s" is missing. So I don't catch a result.
I get selected directory name using in onActivityResult from
Intent intent = new vntent(Intent.ACTION_OPEN_DOCUMENT_TREE)
and then
uri = data.getData();
Documentfile df = DocumentFile.getName();
I get all audiofiles in a folder using:
selection = MediaStore.Audio.Media.BUCKET_DISPLAY_NAME + " like ?";
String[] selectionArgs = new String[] {audiodir};
When I set
selection = MediaStore.Audio.Media.RELATIVE_PATH + " like ?";
String[] selectionArgs = new String[] {audiodir};
I get no audifiles listened.
Total code:
Uri uri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
uri = MediaStore.Audio.Media.getContentUri(MediaStore.VOLUME_EXTERNAL);
}
String directory = "audioFiles"
String audiodir = "%" + directory + "%";
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.DISPLAY_NAME,
MediaStore.Audio.Media.BUCKET_DISPLAY_NAME,
MediaStore.Images.Media.RELATIVE_PATH,
MediaStore.Audio.Media.TRACK,
MediaStore.Audio.Media.YEAR,
};
Cursor cur;
String selection = MediaStore.Audio.Media.RELATIVE_PATH + " like ?";
String[] selectionArgs = new String[] {audiodir};
String sortOder = MediaStore.Audio.Media.TRACK + " ASC";
Cursor cur = getContentResolver().query(uri, projection, selection, selectionArgs, sortOder);
In Android Q mediaStore.audio.media.DATA is depricated so I use mediaStore.audio.media.RELATIVE_PATH instead.
How can I solve this behavior (cut the plural "s" is a bad Workaround).
Thanks,
FP

Select multiple folders from mediaStore

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.

Android ContentResolver does not find music on my device

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);

ListView displaying audio and video from mediastore

I'm trying to create a list that shows both audio and video from the mediastore. However, I'm not sure how to create such a query - is it even possible to get information for both audio and video at the same time?
So to query for video and audio I do this:
String[] projV = { MediaStore.Video.Media._ID,
MediaStore.Video.Media.DATA,
MediaStore.Video.Media.DISPLAY_NAME,
MediaStore.Video.Media.DURATION,
MediaStore.Video.Media.DATE_TAKEN };
Cursor videoCursor = getActivity().managedQuery(
MediaStore.Video.Media.EXTERNAL_CONTENT_URI, projV, null,
null, null);
String[] projA = { MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.MIME_TYPE,
MediaStore.Audio.Media.DURATION,
MediaStore.Audio.Media.DATE_ADDED };
Cursor audioCursor = getActivity().managedQuery(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projA, null,
null, null);
I looked into using CursorJoiner or MergeCursor, but I'm not sure how to use these or even certain its the right solution.
So my question is; Is there a way to construct a query for the mediaStore that returns a cursor with information for both audio and video or do I need to something more complex such as using CursorJoiner or MergeCursor.
As I mentioned at the start, my goal is to have a list displaying all the audio and video in the mediastore - is this the right approach or am I looking at it from a wrong angle?
Thanks.
It is possible from API-11 by using MediaStore.Files with an appropriate selection clause.
public Loader<Cursor> onCreateLoader(int id, Bundle bundle)
{
final String PROJECTION[] = {FileColumns._ID, FileColumns.DATA, FileColumns.DATE_ADDED};
final String ORDER = FileColumns.DATE_ADDED + " DESC";
final String SELECTION = "(" + FileColumns.MEDIA_TYPE + "=" + FileColumns.MEDIA_TYPE_VIDEO +") OR (" + FileColumns.MEDIA_TYPE + "=" + FileColumns.MEDIA_TYPE_IMAGE + ")";
return new CursorLoader(getActivity(), Files.getContentUri("external"), PROJECTION, SELECTION, null, ORDER);
}
Prior to that you can use a MergeCursor if you don't care about sort order. Look at MatrixCursor if you need to manage sort order. Alternatively wrap your cursors in a CursorWrapper that implements order logic.

Categories

Resources