Android Question Check/Make Folder on start

Dogbonesix

Active Member
Licensed User
Longtime User
I would like check for a folder when my app starts and if it does not exist make the folder and a blank sql.db. Every thing works fine in debug mode but the folder/file are not created in the release mode. I have downloaded other apps that create their folders and default data - How can I do the same?
 
This creates folder and file in both Debug and Release ..

Code: Copy
Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
       
        'test for folder and create if not present ..
        Dim MyPath As String : MyPath = File.DirDefaultExternal & "/MyFolder"

        If File.Exists(MyPath, "") = False Then
            File.MakeDir(File.DirDefaultExternal, "MyFolder")

            'as folder was not present , no need to test for DB presence ..  , copy predifined DB from assets to folder
            File.Copy(File.DirAssets, "sql.db", File.DirDefaultExternal & "/MyFolder", "sql.db")
        End If

        'Alternatively create a blank DB file when initializing SQL .. you would then have to defing fields etc in code .

        'initialize the database ..Create new file if does not exist
        'SQL1.Initialize (File.DirDefaultExternal & "/MyFolder" , "sql.db",True)

    End  If
   
End Sub

Maybe upload project if unsolved ..

Cheers mj
 
Upvote 0
Ah Ha, I will give that a try. I was not sure if that "File.DirDefaultExternal" was the External SD card or the file system - it gets a little confusing.

Thanks, I plan on placing the app on the B4A sample apps when I get it competed - it is really simple.
 
Upvote 0
File.DirDefaultExternal and File.DirRootExternal point to the "external storage". On most devices this is not the SD card but rather an additional storage inside the device.

When the compiler sees a call to one of the above folders it adds the WRITE_EXTERNAL permission to the manifest file. This permission is also added automatically in debug mode (this is why it failed in release mode).
 
Upvote 0
The code below works ALL OF TIME when compiled in Debug
------------------------------
MyPath = "/mnt/sdcard/Hyper/MyPics"
If File.Exists(MyPath, "") = False Then
File.MakeDir("/mnt/sdcard/Hyper/MyPics","")
End If
' Check if phone20.dbc exists...
If File.Exists("/mnt/sdcard/Hyper/Data", "phone20.dbc") = False Then
File.Copy(File.DirAssets, "phone20.dbc", "/mnt/sdcard/Hyper/Data", "phone20.dbc")
Msgbox("Created phone20.dbc","")
End If
-----------------------------
It works SOME OF TIME when complied in release mode. It was working in release mode in the morning and later in the day I keep getting an error that the database was in read only mode. I checked the file permissions and they were -rw. So, I rebooted the phone - just in case the database had locked up for some reason - got the same error. Also, some of the time when compiled in release mode the Folders are created and other times not, don't have a clue why. But, it always works - with out error - when compiled in debug mode.
 
Upvote 0
The below does not work... What should I do to add WRITE_EXTERNAL permissions to the manifest file?

SetApplicationAttribute(android:label, "$LABEL$")
AddPermission (android.permission.WRITE_EXTERNAL)
'End of default text.
 
Upvote 0
Back
Top