Android Question Permission Denial error even when the permission is in the manifest [SOLVED]

JohnC

Expert
Licensed User
Longtime User
I have a code module that contain the below code. Even though I have the "android.permission.READ_EXTERNAL_STORAGE" permission in the manifest, I am still getting this error on an Android 8.x device.

B4X:
Sub DoNotification(VibCount As Int, SoundURI As String, Vol As Int)
    Dim V As Float
    Dim Pv As PhoneVibrate
    
    Try
        If Starter.MP.IsPlaying Then
            Starter.MP.Stop
        End If
    Catch
        Log(LastException)
    End Try
    
    'play audio first
    If SoundURI.Trim <> "" Then
        Starter.MP.Load("ContentDir", SoundURI)
        V = Vol / 100
        Starter.MP.SetVolume(V,V)
        Starter.MP.Play
    End If
    
    If VibCount > 0 Then
        Do While VibCount > 0
            Log("VibCount=" & VibCount)
            Pv.Vibrate(1000)
            Sleep(2000)
            VibCount = VibCount - 1
        Loop
    End If
    
End Sub

B4X:
common$ResumableSub_DoNotificationresume (java line: 207)
java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/audio/media/1395 from pid=1452, uid=10120 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
    at android.os.Parcel.readException(Parcel.java:2013)
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
    at android.database.DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(DatabaseUtils.java:146)
    at android.content.ContentProviderProxy.openTypedAssetFile(ContentProviderNative.java:698)
    at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1410)
    at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1247)
    at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1170)
    at anywheresoftware.b4a.objects.MediaPlayerWrapper.loadAfterReset(MediaPlayerWrapper.java:91)
    at anywheresoftware.b4a.objects.MediaPlayerWrapper.Load(MediaPlayerWrapper.java:66)
    at com.comp.appcommon.common$ResumableSub_DoNotification.resume(common.java:207)
    at com.comp.appcommon.common._donotification(common.java:99)
    at com.comp.appcommon.commontimer._tmrmain_tick(commontimer.java:586)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:196)
    at anywheresoftware.b4a.objects.Timer$TickTack.run(Timer.java:105)
    at android.os.Handler.handleCallback(Handler.java:790)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6494)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
 

JohnC

Expert
Licensed User
Longtime User
It looks like only some of the sounds in the ringtone picker will generate this error, and all the other sounds play fine.

For example, if I select "Hangouts Message" it will play the sound when I tap on it while still in the ringtonepicker, but once it returns to my app, I will get this error if I try to play it.

Very weird.
 
Upvote 0

wes58

Active Member
Licensed User
Longtime User
It looks like only some of the sounds in the ringtone picker will generate this error, and all the other sounds play fine.

For example, if I select "Hangouts Message" it will play the sound when I tap on it while still in the ringtonepicker, but once it returns to my app, I will get this error if I try to play it.

Very weird.
RingtonePicker displays all sound files - which are stored on internal and external storage. That's why, if it happens that the sound you select is located on external storage you will get a permission error if it is not granted. You can see in the URI where the file is located.
In your case, from the error, you can see - "uri content://media/external/audio/..."
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
Yes, the permissions fixed the error for typical URI's when using the MediaPlayer.

But I found that some of the default sounds had a URI of "content://settings/system/ringtone", which media player gave this error:
B4X:
(FileNotFoundException) java.io.FileNotFoundException: Direct file access no longer supported; ringtone playback is available through android.media.Ringtone
So, it looks like those types of sounds will play with the RTM.Play method only.
 
Upvote 0
Top