Android Question MediaStore - should this code work right?

MrKim

Well-Known Member
Licensed User
Longtime User
I am trying to get a list of all the Audio files on a device with the following code but I am baffled. On one device it returns the files from ONE directory but there are a dozen directories all in the same place and there are files in the PARENT of the returned directory and they are not returned. All files were copied to the device from windows at the same time. On another device I get the same result + the addition of all the files in the File path: /storage/emulated/0/Notifications AND a number of files in several subdirectories of Notifications that are hidden in file manager. I can get what I need with external storage but it is incredibly slow. One source said you need to force media manager to refresh and I found code which purported to do that (thank you Copilot) but it made no difference (Copilot, Oh well). I can get what I need with External Storage lib but it is slow. Any thoughts and ideas appreciated.

Pixel 7/Samsung Galaxy 7 lite are what I am testing on.

You can see in the commented code a partial list of the things I tried none of which made a difference.

B4X:
   ' Obtain application context.
    Dim context As JavaObject
    context.InitializeContext

    ' Get the ContentResolver from the context.
    Dim cr As JavaObject = context.RunMethod("getContentResolver", Null)

    ' Initialize the MediaStore.Audio.Media static class.
    Dim mediaStoreAudio As JavaObject
    mediaStoreAudio.InitializeStatic("android.provider.MediaStore$Audio$Media")

    ' Get the external audio content URI.
    Dim uri As Object = mediaStoreAudio.GetField("EXTERNAL_CONTENT_URI")

    ' Define the projection columns.
'    Dim projection() As String = Array As String("TITLE", "_data", "DURATION", "ARTIST", "ALBUM")
    Dim projection() As String = Array As String("TITLE", "_data", "DURATION", "ARTIST", "ALBUM")

    ' Perform the query.
'    Dim selection As String = "MIME_TYPE LIKE 'audio/%'"
'    Dim selection As String = "_data LIKE ?"
    ' The selection argument uses a wildcard (%) after the directory path.
'    Dim selectionArgs() As String = Array As String("audio" & "/%")

'    Dim cursor As JavaObject = cr.RunMethod("query", Array(uri, projection, selection, selectionArgs, Null))
    Dim cursor As JavaObject = cr.RunMethod("query", Array(uri, projection, Null, Null, Null))
    
    If cursor = Null Then
        Log("Cursor is null; check that permissions are granted and MediaStore has scanned the files.")
        Return
    End If
 
Solution
The solutions is HERE I am using Runtime permissions. I made the assumption that any required permissions would have a RP constant but evidently the don't. I had to use
B4X:
rp.CheckAndRequest("android.permission.READ_MEDIA_AUDIO")
The reason I was getting SOME files is because they were actually ringtones, which although part of the audio media DB evidently do NOT require permissions (go figure).

MrKim

Well-Known Member
Licensed User
Longtime User
Do you see the audio files in the device player app?

Your code looks correct.
Yes, all of the files are there but have to be negotiated directory by directory. The same is true of File Manager +. I am trying to get a list of ALL of the music files. I have looked at a couple of the other apps here with media browsers and they give the same result.
Well, I got it working. I had
B4X:
AddPermission(android.permission.READ_MEDIA_AUDIO)
in the manifest but for some reason when looking at the app it said no permissions required so I assumed this was a "Safe" permission and so wasn't listed. Interestingly Allow Notifications was disabled (NOT turned off, disabled) for some reason as well.

Evidently my overnight turn device off/on changed that. Today "Music and Audio" was in the app permission list and once enabled I got all of my files.

The other thing that threw me off however was the fact that I did get SOME files. Just not all when i didn't have permissions-bizarre. Also I didn't see READ_MEDIA_AUDIO in the runtime permissions library. But I will post another question about that..
 
Last edited:
Upvote 0

MrKim

Well-Known Member
Licensed User
Longtime User
The solutions is HERE I am using Runtime permissions. I made the assumption that any required permissions would have a RP constant but evidently the don't. I had to use
B4X:
rp.CheckAndRequest("android.permission.READ_MEDIA_AUDIO")
The reason I was getting SOME files is because they were actually ringtones, which although part of the audio media DB evidently do NOT require permissions (go figure).
 
Upvote 1
Solution
Top