Android Question GetSafeDirDefaultExternal with errors

Scantech

Well-Known Member
Licensed User
Longtime User
This is my first time experimenting with GetSafeDirDefaultExternal. I have a device where i can mount and eject secondary storage. With the following codes

B4X:
        Dim DirFile As String
        DirFile = rp.GetSafeDirDefaultExternal("Test APP")

        'If no database file then create it and init it.
        If File.Exists(DirFile, "My1File.db") = False Then
            SQL1.Initialize(DirFile, "My1File.db", True)
            Query = "CREATE TABLE diagnosticlog (ID INTEGER PRIMARY KEY, RO TEXT, Date TEXT, FirstName TEXT, LastName TEXT, VIN TEXT, Year TEXT, Make TEXT, Model TEXT, FileName TEXT)"
            SQL1.ExecNonQuery(Query)
            'file is available just init it
        Else
            If SQL1.IsInitialized = False Then
                SQL1.Initialize(DirFile, "My1File.db", True)
            End If
        End If

When i Mount the secondary storage it is successfuly with creating the .db file. But, when i eject the Secondary Storage it fails to create (Should create in DirInternal Correct?).

*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Error occurred on line: 65 (Main)
android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:808)
at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:793)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:696)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:671)
at anywheresoftware.b4a.sql.SQL.Initialize(SQL.java:44)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:777)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:354)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:180)
at anywheresoftware.b4a.debug.Debug.delegate(Debug.java:262)
at b4a.example.main._createdatabase(main.java:397)
at b4a.example.main._activity_create(main.java:389)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at b4a.example.main.afterFirstLayout(main.java:104)
at b4a.example.main.access$000(main.java:17)
at b4a.example.main$WaitForLayout.run(main.java:82)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6244)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:887)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:777)
Error CreateDatabase Possibly permission not enabled: java.lang.Exception: android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
 

Mahares

Expert
Licensed User
Longtime User
When i Mount the secondary storage it is successfuly with creating the .db file. But, when i eject the Secondary Storage it fails to create (Should create in DirInternal Correct?)
B4X:
Dim DirFile As String 
    If File.ExternalWritable Then    'check for external storage
        DirFile = rp.GetSafeDirDefaultExternal("TestAPP")    'I prefer not to have spaces in the folder name. You nay get away with Test APP
    Else
        DirFile = File.DirInternal
    End If
 
Upvote 0

Scantech

Well-Known Member
Licensed User
Longtime User
I recommend using File.DirInternal unless there is a special reason to use the secondary storage.
Clearing the Storage Data in Android Settings will delete any files created in File.DirInternal? That is why i prefer secondary storage first.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Clearing the Storage Data in Android Settings will delete any files created in File.DirInternal?
All the app folders will be deleted. This includes rp.GetSafeDirDefaultExternal.
I've just tried it.

You can request the storage permission at runtime and write outside of your app folders. However this is not something that most apps should do.
 
Upvote 0
Top