Android Question sqlite save to external memory card

kgf

Member
Licensed User
This is what I have used, working under android:targetSdkVersion="29" and SetApplicationAttribute(android:requestLegacyExternalStorage, true) but unsure if will continue to work under newer versions.

B4X:
Sub backupSqliteFile
    ' create file with day of the week at the end  ie  *.sqlite.bak.Friday
     ' sql, dbFolder location and and dbFile name declared in starter
    ' close db first
    Starter.sql.Close
    Dim folders As List = Starter.rp.GetAllSafeDirsExternal("")
    Dim BackupFolder As String
    'Log(folders)
    Try
        Select Case folders.Size
            Case 1
                'backup locally if no sd card
                BackupFolder = folders.Get(0)
            Case 2
                'backup to SD Card into Android\data app folder
                BackupFolder = folders.Get(1)
            Case Else
        End Select
        Log("Backup Folder is " & BackupFolder)
        ' make "backup" folder
        If Not(File.Exists(BackupFolder, "backup")) Then
            Log("Make Dir "& "backup")
            File.MakeDir(BackupFolder, "backup")
        Else
            Log("backup folder Exists")
        End If
        ' copy database
        File.Copy(Main.SafeDirDefaultExternalPath & "/" & Starter.dbFolder,  Starter.dbFolder, BackupFolder & "/" & "backup", Starter.dbFile & TodayWeekDay & ".bak.")
    Catch
        Log(LastException)
    End Try
    ' re int db
    If Starter.sql.IsInitialized Then
    Else
        Starter.sql.Initialize(Main.SafeDirDefaultExternalPath & "/" & Starter.dbFolder, Starter.dbFile, False)
    End If
End Sub

Public Sub TodayWeekDay() As String
    Dim lNow As Long
    lNow = DateTime.Now
    Return DateUtils.GetDayOfWeekName(lNow)
End Sub
 
Upvote 0
Top