Android Question rp.GetAllSafeDirsExternal("") [Wrong idea--- ignore]

Roger Daley

Well-Known Member
Licensed User
Longtime User
Hi All,

Trying to sort out RunTime Permissions as per the tutorial and getting tripped up by replacing "File.DirRootExternal" with "rp.GetAllSafeDirsExternal("")" for older devices.
Below is the code that is causing the problem.

I need to translate:
File.Exists(File.DirRootExternal&"/ABT/", "") Etc.
to something like this:
File.Exists(rp.GetAllSafeDirsExternal("/ABT/"),"") Etc.

As it is it generates a number of warnings and of course does not compile.
If some kind person can spot what I have done wrong please point me in the right direction.

Regards Roger
[My other solution is to set the SDKMin to 21. [Android version 5+]

B4X:
 '***  Checks for Permission to read/write external memory and creates directory to save files. ***
        If rp.Check("android.permission.WRITE_EXTERNAL_STORAGE") = False Then
            rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
            Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
            
            If Permission =  "android.permission.WRITE_EXTERNAL_STORAGE" And Result =  True  Then
                
                'If File.Exists(File.DirRootExternal&"/ABT/", "") = False Then File.MakeDir(File.DirRootExternal&"/ABT/", "")
                If File.Exists(rp.GetAllSafeDirsExternal("/ABT/"),"") = False Then
                    File.MakeDir(rp.GetAllSafeDirsExternal("/ABT/"))
                End If
                
            End If
            
        Else
            
            'If File.Exists(File.DirRootExternal&"/ABT/", "") = False Then File.MakeDir(File.DirRootExternal&"/ABT/", "")
            If File.Exists(rp.GetAllSafeDirsExternal("/ABT/"), "") = False Then File.MakeDir(rp.GetAllSafeDirsExternal("/ABT/"))
        
        End If
 

DonManfred

Expert
Licensed User
Longtime User
Try it this way
B4X:
rp.GetAllSafeDirsExternal("ABT")

You dont need runtimepermission for this folder as you are not writing to DirRootExternal

https://www.b4x.com/android/help/ru...#runtimepermissions_getsafedirdefaultexternal


NO NEED to create the subfolder by yourself
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User


Hi DonManfred,

Thanks for the feedback.
Unfortunately "rp.GetAllSafeDirsExternal("ABT")" is one of several variations I have already tried without success.

I think there may be some confusion over the folder.
First this is a change to an existing App where the ABT folder is already in use.
The ABT folder is created by the App to store files created by the user. ABT is not the sub folder refered to in Erels tutorial.

You wrote"
"You dont need runtimepermission for this folder as you are not writing to DirRootExternal"
As I am creating a folder [ABT] in DirRootExternal surely this means I am writing to DirRootExternal?


I have already added the code to the manifest but just stuck on the "rp.GetAllSafeDirsExternal("")" bit.
B4X:
AddManifestText(<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="19" />)


Thanks again for getting back. I will keep trying to find an example of "rp.GetAllSafeDirsExternal("")" in the context I am using it.

Regards Roger
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
rp.GetAllSafeDirsExternal("") returns a very specific folder. You can log it to see.

It will not help you with accessing the existing folder. You need to request the external storage permission at runtime. Make sure to remove this snippet:
B4X:
AddManifestText(<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="19" />)
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User


Thanks Erel,
I never thought the folder ABT was related to the sub folder mentioned in your app.
The code I posted is requesting the external runtime permission at run time and everything works in it's original form but from the tutorial it appeared I needed the code snippet in the manifest and to change:
'If File.Exists(File.DirRootExternal&"/ABT/", "") = False Then File.MakeDir(File.DirRootExternal&"/ABT/", "")
to something like:
If File.Exists(rp.GetAllSafeDirsExternal("/ABT/"), "") = False Then File.MakeDir(rp.GetAllSafeDirsExternal("/ABT/
for devices pre-android version 5.


I can can only assume I have confused myself in the tutorial and I will return to my original code. I don't have an Android device pre-version 5 so I will just have to see if a user has a problem.

I have removed the snippet from the manifest.
I have returned the code to the original as shown below.

B4X:
      '*****  Checks for Permission to read/write external memory and creates directory to save files.  ************
        If rp.Check("android.permission.WRITE_EXTERNAL_STORAGE") = False Then
            rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
            Wait For Activity_PermissionResult (Permission As String, Result As Boolean)          
            If Permission =  "android.permission.WRITE_EXTERNAL_STORAGE" And Result =  True  Then              
                If File.Exists(File.DirRootExternal&"/ABT/", "") = False Then File.MakeDir(File.DirRootExternal&"/ABT/", "")
            End If          
        Else  
            If File.Exists(File.DirRootExternal&"/ABT/", "") = False Then File.MakeDir(File.DirRootExternal&"/ABT/", "")
        End If

Regards Roger
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…