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

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.

Related

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.

MediaStore to list from specific path

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

MediaStore.Audio.Media.TITLE, unreadable text

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?

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

Categories

Resources