Android Question Can't create folders anymore on my mobile

Fab117

Member
Licensed User
Longtime User
Hi,

Long time ago I created an app that I'd like to upgrade.

But I'm already blocked at the 1st stage, I can't create the subfolders on the mobile (ideally in "/storage/emulated/0"

Here is the code I had in Starter:
Starters:
Sub Process_Globals
    ' Repertoire de stockage des données
    Public EmplacementStockageSurDevice As String : EmplacementStockageSurDevice = File.DirRootExternal 'EmplacementStockageSurDevice : /storage/emulated/0
    Public RepertoireRacine As String : RepertoireRacine = "/Toto App"
    Public NomApplication As String : NomApplication = "/First App"
End Sub

Sub Service_Create
   
    '___________________________________________________________________
    '<<<======================
    ' ______________Création des Répertoires nécessaires sur le Device et copie des fichiers langues et du fichier ini_____________________________
    ' ==========================>>>
    ' Création des répertoires pour stocker la base de données, les photos et les fichiers langues si nécesaire
    If File.Exists(EmplacementStockageSurDevice & RepertoireRacine & NomApplication & "/Pictures", "") = False Then
        File.MakeDir(EmplacementStockageSurDevice, RepertoireRacine & NomApplication & "/Pictures")
    End If
    If File.Exists(EmplacementStockageSurDevice & RepertoireRacine & NomApplication & "/Data Base", "") = False Then
        File.MakeDir(EmplacementStockageSurDevice, RepertoireRacine & NomApplication & "/Data Base")
    End If
    If File.Exists(EmplacementStockageSurDevice & RepertoireRacine & NomApplication & "/Language support files", "") = False Then
        File.MakeDir(EmplacementStockageSurDevice, RepertoireRacine & NomApplication & "/Language support files")
    End If
    ' Création du répertoires pour stocker les photos encryptées si nécesaire
    If File.Exists(EmplacementStockageSurDevice & RepertoireRacine & NomApplication & "/tmp/Pictures/Zip", "") = False Then
        File.MakeDir(EmplacementStockageSurDevice, RepertoireRacine & NomApplication & "/tmp" & "/Pictures" & "/Zip")
    End If
    ' Création du répertoires pour stocker la base de données encryptées si nécesaire
    If File.Exists(EmplacementStockageSurDevice & RepertoireRacine & NomApplication & "/tmp/Data Base/Zip", "") = False Then
        File.MakeDir(EmplacementStockageSurDevice, RepertoireRacine & NomApplication & "/tmp" & "/Data Base" & "/Zip")
    End If
    ' Création du répertoires pour stocker les pdf
    If File.Exists(EmplacementStockageSurDevice & RepertoireRacine & NomApplication & "/PDFs", "") = False Then
        File.MakeDir(EmplacementStockageSurDevice, RepertoireRacine & NomApplication & "/PDFs")
    End If

End Sub


I spend really long time on the "B4X Getting Started" & "B4X Basic Language" (chap 5.13.1.1.2) guides and on the forum (e.g. https://www.b4x.com/android/forum/threads/solved-file-dirrootexternal-and-runtimepermissions.90780/), but despite lot of trials, didn't solve it.

I also tried to create them on "File.DirDefaultExternal" (which could be an option for me, but less valuable), but was also unsuccessful.



Would someone be able to help me on this?



Thanks.



Fab
 

JohnC

Expert
Licensed User
Longtime User
Have you tried this post (just the last post):

 
Upvote 0

carlbertech

Member
Hi,

Long time ago I created an app that I'd like to upgrade.

But I'm already blocked at the 1st stage, I can't create the subfolders on the mobile (ideally in "/storage/emulated/0"

Here is the code I had in Starter:
Starters:
Sub Process_Globals
    ' Repertoire de stockage des données
    Public EmplacementStockageSurDevice As String : EmplacementStockageSurDevice = File.DirRootExternal 'EmplacementStockageSurDevice : /storage/emulated/0
    Public RepertoireRacine As String : RepertoireRacine = "/Toto App"
    Public NomApplication As String : NomApplication = "/First App"
End Sub

Sub Service_Create
  
    '___________________________________________________________________
    '<<<======================
    ' ______________Création des Répertoires nécessaires sur le Device et copie des fichiers langues et du fichier ini_____________________________
    ' ==========================>>>
    ' Création des répertoires pour stocker la base de données, les photos et les fichiers langues si nécesaire
    If File.Exists(EmplacementStockageSurDevice & RepertoireRacine & NomApplication & "/Pictures", "") = False Then
        File.MakeDir(EmplacementStockageSurDevice, RepertoireRacine & NomApplication & "/Pictures")
    End If
    If File.Exists(EmplacementStockageSurDevice & RepertoireRacine & NomApplication & "/Data Base", "") = False Then
        File.MakeDir(EmplacementStockageSurDevice, RepertoireRacine & NomApplication & "/Data Base")
    End If
    If File.Exists(EmplacementStockageSurDevice & RepertoireRacine & NomApplication & "/Language support files", "") = False Then
        File.MakeDir(EmplacementStockageSurDevice, RepertoireRacine & NomApplication & "/Language support files")
    End If
    ' Création du répertoires pour stocker les photos encryptées si nécesaire
    If File.Exists(EmplacementStockageSurDevice & RepertoireRacine & NomApplication & "/tmp/Pictures/Zip", "") = False Then
        File.MakeDir(EmplacementStockageSurDevice, RepertoireRacine & NomApplication & "/tmp" & "/Pictures" & "/Zip")
    End If
    ' Création du répertoires pour stocker la base de données encryptées si nécesaire
    If File.Exists(EmplacementStockageSurDevice & RepertoireRacine & NomApplication & "/tmp/Data Base/Zip", "") = False Then
        File.MakeDir(EmplacementStockageSurDevice, RepertoireRacine & NomApplication & "/tmp" & "/Data Base" & "/Zip")
    End If
    ' Création du répertoires pour stocker les pdf
    If File.Exists(EmplacementStockageSurDevice & RepertoireRacine & NomApplication & "/PDFs", "") = False Then
        File.MakeDir(EmplacementStockageSurDevice, RepertoireRacine & NomApplication & "/PDFs")
    End If

End Sub


I spend really long time on the "B4X Getting Started" & "B4X Basic Language" (chap 5.13.1.1.2) guides and on the forum (e.g. https://www.b4x.com/android/forum/threads/solved-file-dirrootexternal-and-runtimepermissions.90780/), but despite lot of trials, didn't solve it.

I also tried to create them on "File.DirDefaultExternal" (which could be an option for me, but less valuable), but was also unsuccessful.



Would someone be able to help me on this?



Thanks.



Fab
add this line to manifest editor
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="28"/>

before write chechk for permissions :
' --- Permiso Write external storage
rp.CheckAndRequest (rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
Wait For B4XPage_PermissionResult (Permission As String, R As Boolean)
If R=False Then
ToastMessageShow("Sin permiso R/W",True)
Return
End If

This work on Android 11 and B4A 11
 
Upvote 0

Fab117

Member
Licensed User
Longtime User
Hello,

Still not solved.

With the Manifest Editor, I replaced line 5 from "<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="29"/>" to "<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="28"/>"

In starter, I added:
Starter:
Sub Service_Create
    'This is the program entry point.
    'This is a good place to load resources that are not specific to a single activity.
    
    ' --- Permiso Write external storage
    Dim rp As RuntimePermissions
    rp.CheckAndRequest (rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
    Wait For B4XPage_PermissionResult (Permission As String, R As Boolean)
    If R=False Then
        ToastMessageShow("Sin permiso R/W",True)
        Return
    End If

End Sub


And here is the error message I got:

Error Message.png



NB: My mobile has no external SD-Card (I would like to create folders on "/storage/emulated/0" and is running under Android 9 PKQ1.180904.001
NB2: I attach the demo project I created to test folder creation


Fab
 

Attachments

  • 03 Folder creation.zip
    245.6 KB · Views: 32
Upvote 0

Fab117

Member
Licensed User
Longtime User
Hopefully solved.

Thank you very much for your support


Methodology in case someone face the same challenge:
  1. Open Manifest Editor
  2. Replace ligne <uses-sdk android:minSdkVersion="X" android:targetSdkVersion="XX"/> by <uses-sdk android:minSdkVersion="5" android:targetSdkVersion="28"/>
  3. Not in Starter => e.g. in "Main":
Main:
Sub Activity_Create(FirstTime As Boolean)
    FolderCreation
End Sub

Sub FolderCreation
    Dim rp As RuntimePermissions
    rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
    If Result Then
        File.MakeDir(File.DirRootExternal,"Fabdd/772")': /storage/emulated/0
        MsgboxAsync("Folder created", "Hopefuly")
    Else
        MsgboxAsync("Impossible to create folders", "Error")
    End If
End Sub


Regards,

Fabrice
 
Last edited:
Upvote 0

Fab117

Member
Licensed User
Longtime User
You can not upload this app to google Playstore using targetsdk = 28. Is this intentional/expected?
Hi DonManfred,
No it's not intentional.
For now I upgrade my app only for me, but would be happy to share it on the PlayStore if I can finalize it.
Should I proceed differently?

Thanks.

Fab
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Should I proceed differently?
Sure. TargetSDK must be 30.

 
Upvote 0

agraham

Expert
Licensed User
Longtime User
If you target SDK 30 it will limit your access to the file system. See the section for SDK29 and higher in the DonManfred link above.

 
Upvote 0

Fab117

Member
Licensed User
Longtime User
Dear Both,
Thank you very much looking at my issue.
In the Manifest Editor, I changed the targetSdkVersion to 30.
I still can create folders in File.DirRootExternal

Main:
Sub Activity_Create(FirstTime As Boolean)
    FolderCreation
End Sub

Sub FolderCreation
    Dim rp As RuntimePermissions
    rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
    If Result Then
        File.MakeDir(File.DirRootExternal,"Fabnnn/rrr")': /storage/emulated/0
        MsgboxAsync("Folder created", "Hopefuly")
    Else
        MsgboxAsync("Impossible to create folders", "Error")
    End If
    
    copiefichier
End Sub


However, I can't copy here files from File.DirAssets

Main:
Sub copiefichier
    Private lstFiles As List
    lstFiles = File.ListFiles(File.DirAssets)

For i = 0 To lstFiles.Size - 1
    Dim FileName As String
    FileName = lstFiles.Get(i)
    If FileName = "Test.txt" Then
        File.Copy(File.DirAssets, FileName, File.DirRootExternal &"Fabnnn/rrr", FileName)
    End If
Next

End Sub


Looking at your link (and further ones) did I understand correctly that the only way is t have the user to select the folder?


Would it be simpler if instead File.DirRootExternal, I would use File.DirDefaultExternal to store the files I need in my app?

File.DirInternal is not an option, because I need simple access to the storage folder when connect to my computer (with Windows file explorer).



To be very clear on what I try to do:
  • My app must copy some files from File.DirAssets to "easily accessible" folders on my mobile.
  • Then the user (through the app) will do changes on those files (database). NB: The user will also generate new files (pictures; pdf) in those folders
Thanks

Fab
 
Upvote 0
Top