Android Question How to implement runtime permission to copy file to internal in android 13

hicaltech87

Member
I'm in update my apps to compatible in android 13
the problem is when i follow this notification permission https://www.b4x.com/android/forum/threads/notifications-permission-with-targetsdkversion-33.148233/
The apps become crash when i click on that menu
is my code is correct?

B4X:
Dim filedir As String = mainmenu.rp.GetSafeDirDefaultExternal("data")
    mainmenu.rp.CheckAndRequest(mainmenu.rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
    Wait For (CheckAndRequestNotificationPermission) Complete (Result As Boolean)
    If Result Then
        Dim n As Notification
        n.Initialize
        n.Icon = "icon"
        n.SetInfo("Permission", "App Need Your Permission to Save Database.", Me) 'Change Main to "" if this code is in the main module.
        n.Notify(1)
        If File.Exists(filedir,mainmenu.filedb) = False Then
            File.Copy(File.DirAssets,mainmenu.filedb,filedir,mainmenu.filedb)
            End If
        mainmenu.sql.Initialize(filedir,mainmenu.filedb,True)
    End If
    setedittext
End Sub
Private Sub CheckAndRequestNotificationPermission As ResumableSub
    Dim ps As Phone
    If ps.SdkVersion < 33 Then Return True
    Dim ctxt As JavaObject
    ctxt.InitializeContext
    Dim targetSdkVersion As Int = ctxt.RunMethodJO("getApplicationInfo", Null).GetField("targetSdkVersion")
    If targetSdkVersion < 33 Then Return True
    Dim NotificationsManager As JavaObject = ctxt.RunMethod("getSystemService", Array("notification"))
    Dim NotificationsEnabled As Boolean = NotificationsManager.RunMethod("areNotificationsEnabled", Null)
    If NotificationsEnabled Then Return True
    Dim rp As RuntimePermissions
    rp.CheckAndRequest(rp.PERMISSION_POST_NOTIFICATIONS)
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean) 'change to Activity_PermissionResult if non-B4XPages.
    Log(Permission & ": " & Result)
    Return Result
End Sub
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Tip: whenever you see code with PERMISSION_WRITE_EXTERNAL_STORAGE you immediately know that it is broken.

The apps become crash when i click on that menu
Post the error message with the stack trace.

is my code is correct?
No. You don't need to request permission to access GetSafeDirDefaultExternal, however there is no good reason to use that folder. Use File.DirInternal = XUI.DefaultFolder instead.
 
Upvote 0

hicaltech87

Member
So, i don't need to use
B4X:
Dim filedir As String = mainmenu.rp.GetSafeDirDefaultExternal("data")
mainmenu.rp.CheckAndRequest(mainmenu.rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
Use File.DirInternal = XUI.DefaultFolder instead.
and change to this code File.DirInternal = XUI.DefaultFolder right?
Thanks erel, i will try to use that,,
 
Upvote 0
Top