Android Question Permissions in B4XPage

Sergey_New

Well-Known Member
Licensed User
Longtime User
I'm trying to set permission in the Main module. The permission is set, and then I need to create a folder with the name of the application. The folder is not created.
Main:
Sub Activity_Create(FirstTime As Boolean)
    Dim pm As B4XPagesManager
    pm.Initialize(Activity)
    rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
    If Result Then
        File.MakeDir(File.DirRootExternal, "MyPages")
    Else
        Log("No permission!")
    End If
End Sub
What can I do?
Example attached.
 

Attachments

  • MyPages.zip
    9.4 KB · Views: 109
Last edited:

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Hi. Just access the Android/Data/b4a.example/Files (i.e. the DirRootExternal) using rp.GetSafeDirExternal(""), like in the following code:
B4X:
Sub Activity_Create(FirstTime As Boolean)
    Dim pm As B4XPagesManager
    pm.Initialize(Activity)
    rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean) 
    If Result Then
        File.MakeDir(rp.GetSafeDirDefaultExternal(""), "MyPages") '###
    Else
        Log("No permission!")
    End If
End Sub
This works in you case. Moreover, in the past, I also had problems with the Wait For Activity_permissionResult. I actually use
B4X:
Wait For b4xpage_PermissionResult (Permission As String, Result As Boolean)
despite this seems not to be necessary in your case, because i checked that the folder is created, without this last correction.. Just keep also this detail in mind for any case.
Bye
 
Upvote 0

Sergey_New

Well-Known Member
Licensed User
Longtime User
I don't understand, but I don't have a result in all the proposed options, neither on the device nor on the emulator.
Please create a working example.
This one work directly without need to request any permission.
I use targetSdkVersion="29", that's why I request permission.
 
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
Here is an example.
But this will let you create files only inside the app folder.
These files will be visible and accessible only by the app itself.
You can later check and download (or upload files) to the folder by connecting the phone to a pc and going to the folder.
Using SDK 29 and not having the app on market you could try adding this to the Manifest
B4X:
SetApplicationAttribute(android:requestLegacyExternalStorage, true)
and use your code.
 

Attachments

  • FolderTest.zip
    9.2 KB · Views: 85
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
The example that I posted is what you need?
If yes you can do the same even in a B4XPage project.
 
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
This should be what you need.
Check the Manifest.
 

Attachments

  • Folder.zip
    14 KB · Views: 87
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
Main module in B4XPages should not be modified if not in some particular case.
What's the problem doing it in the B4XMainPage?
Maybe we can find a better solution if you have a particular need.
 
Upvote 0

Sergey_New

Well-Known Member
Licensed User
Longtime User
if you have a particular need
I need to launch one or another page after creating the necessary folder. B4XMainPage is needed as the main page of the application, where the main functions of the application are located. But sometimes, depending on the application settings, it is necessary to first open another page (in particular, when installing the application for the first time). Maybe I explained it somehow confusingly, but I could not do it better :)
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I need to launch one or another page after creating the necessary folder. B4XMainPage is needed as the main page of the application, where the main functions of the application are located. But sometimes, depending on the application settings, it is necessary to first open another page (in particular, when installing the application for the first time). Maybe I explained it somehow confusingly, but I could not do it better :)
Unless I'm mistaken, B4XMainPage is always the entry point of a B4XPages project, you can't change it.
You can not show B4XMainPage and show another page right away, but the B4XPage_Created of B4XMainPage will always be executed.
 
Upvote 0

Sergey_New

Well-Known Member
Licensed User
Longtime User
I came across the question and solution about two pages on the forum, but I couldn’t find it.
 
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
As @LucaMs wrote B4XMainpage will always be executed.
After getting the permission and creating the folder then you can show all the other pages you need.
You can move the permission request even in the other pages, or even duplicate multiple time in multiple pages the request.
 
Upvote 0

Sergey_New

Well-Known Member
Licensed User
Longtime User
You can not show B4XMainPage and show another page right away, but the B4XPage_Created of B4XMainPage will always be executed.
What is the best way to do it in the following case:
When opening the application, you need to first set its settings, then go to the main page. I understand that this needs to be done in B4XMainPage, but in this case I would like the content of B4XMainPage not to be displayed or to differ from its main content.
 
Upvote 0

Sergey_New

Well-Known Member
Licensed User
Longtime User
Post #10 Android Tutorial B4XPages explains how to create a page in the B4XMainPage class that will be displayed first. Unfortunately, I don't understand how to do this, and I couldn't find an example.
Please help with an example.
 
Upvote 0

Alex Guerrero

Member
Licensed User
Longtime User
I don't know if you are referring to this command

MakeDir:
        If File.Exists(File.DirRootExternal & "/Surveyfotos/", "") = False Then
            File.MakeDir(File.DirRootExternal , "/Surveyfotos/")
        End If
 
Upvote 0
Top