Android Question B4XPage_PermissionResult was not raised

Hi, everyone,
In a project I am working on, b4xpage_PermissionResult doesn't seem to fire after I request permission to write external storage. The dialog box asking for permission has never shown up.


My code is like this:
B4A:
Private Sub B4XPage_Created (Root1 As B4XView)
    Log("------------B4XPage_Created---------------- :")
    Root = Root1
    'load the layout to Root
    Root.LoadLayout("page2")
    pms.CheckAndRequest(pms.PERMISSION_WRITE_EXTERNAL_STORAGE)
    Wait For B4XPage_PermissionResult(Permission As String, Result As Boolean)

End Sub

Private Sub B4XPage_PermissionResult (Permission As String, Result As Boolean)   'There was no output of this sub to the logger
    Log("------B4XPage_PermissionResult-------- :")
    Log($"    Permission=${Permission}
    Result=${Result}"$)
End Sub

Have I done something wrong?
 

Sagenut

Expert
Licensed User
Longtime User
Did you declared the permission even in the Manifest?
B4X:
AddManifestText(
<uses-permission
  android:name="android.permission.WRITE_EXTERNAL_STORAGE"
  android:maxSdkVersion="19" />
)
 
Upvote 0
Did you declared the permission even in the Manifest?
B4X:
AddManifestText(
<uses-permission
  android:name="android.permission.WRITE_EXTERNAL_STORAGE"
  android:maxSdkVersion="19" />
)
I didn't because I had incorrectly assumed that requesting runtime permissions did not require editing the Manifest。 After adding the permission in the Manifest, the dialog box asking for permission now displays correctly. The only problem now is that the B4XPage_PermissionResult sub is still not triggered.
 
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
Try
B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    Log("------------B4XPage_Created---------------- :")
    Root = Root1
    'load the layout to Root
    Root.LoadLayout("page2")
    pms.CheckAndRequest(pms.PERMISSION_WRITE_EXTERNAL_STORAGE)
    Wait For B4XPage_PermissionResult(Permission As String, Result As Boolean)
    If Result then
        Log(Permission & " granted (" & Result & ")")
    Else
        Log(Permission & " refused (" & Result & ")")
    End If
End Sub
But you should check what @Erel pointed out.
Take this code just as the example to request permissions in general.
 
Upvote 0
The external storage permission is no longer relevant. You cannot directly access the external storage: https://www.b4x.com/android/forum/threads/android-jar-targetsdkversion-minsdkversion.87610/

See this thread: SaveAs - Let the user select a target folder + list of other related methods
Thanks, will try to use the libraries you recommend in future projects.
Just minutes ago, I finally located the cause of the problem(with the help of ChatGPT), I suppose. The Wait For sentence might have caused an event loss(or something similar).

B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    '...
    Wait For B4XPage_PermissionResult(Permission As String, Result As Boolean)'This line caused event loss.
    '...
end sub

Once I remove that line, then B4XPage_PermissionResult can be raised as expected.
 
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
Without the Wait For the code will continue it's execution without waiting for the user to allow or deny for the permission.
In your case it can work because the request is the last instruction of the sub.
But it's not the normal case, in my opinion.
 
Upvote 0
Without the Wait For the code will continue it's execution without waiting for the user to allow or deny for the permission.
In your case it can work because the request is the last instruction of the sub.
But it's not the normal case, in my opinion.
You are right. It seems that I better try what Erel recommended.
 
Upvote 0
Top