Android Question Data backup on SD card

strupp01

Active Member
Licensed User
Longtime User
Since I have been trying for several days to find a solution with examples available online, I have now given up.:(
I need help.
I have a Samsung tablet with an inserted SD card. I want to write a database to the SD card as a backup.
There is a "Mein Haushaltsbuch" directory on the tray. It contains the file "Daten_Mein_Haushaltsbuch.db". I would like to copy this database onto the SD card in the "Sicherung Mein Haushaltsbuch" directory. If this directory does not yet exist, it should be created.
It should not be selected, but should be programmed with these fixed names.
I hope you can help me and expand my knowledge of B4A. Thanks in advance and greeting
strupp01
 

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

strupp01

Active Member
Licensed User
Longtime User
With your example, I tried to program a solution for me.
I didn't make it. :(:(
The example is with some file lists from which a file is selected. Since I do not want that, but want to work with fixed file names and directories, I did not get this programmed.
Therefore I need help !!!
 
Upvote 0

strupp01

Active Member
Licensed User
Longtime User
Again, I tried for several days and got no result. I don't give up so quickly, but here is my Latin. Maybe it is also because of my age that I can no longer find the solution.
I was glad about your help. However, it is not enough for me at this point.
 
Upvote 0

strupp01

Active Member
Licensed User
Longtime User
I was able to create a program to back up the data. The class module "ExternalStore" is required.
It's not quite what I want it to be, but it works like this for now.
What is still missing is the creation of the backup folder, if it does not already exist. Secondly, I would like to have the selection of the backup folder permanently stored in the program and not select it in the menu as now.
Maybe someone can help and tell me the crucial changes.

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

    Private Storage As ExternalStorage
    Private FoldersStack As List
    Private UpItem As ExternalFile
    Private ctxt As JavaObject

End Sub
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private root As ExternalFile
    Private PersistantUri As String
    Private const FileName As String = "PersistantUri"


End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")

    ctxt.InitializeContext

    If FirstTime Then
        Storage.Initialize (Me, "Storage")
        FoldersStack.Initialize
        UpItem.Initialize
    End If

    If File.Exists(File.DirInternal, FileName) Then
        PersistantUri = File.ReadString(File.DirInternal, FileName)
        Log($"PersistantUri=${PersistantUri}"$)
    End If

    
    
    Storage.SelectDir(False)
    Wait For Storage_ExternalFolderAvailable
    
    Dim lFileName As String="Daten_Mein_Haushaltsbuch.db"
    Dim InputStream1 As InputStream
    InputStream1 = File.OpenInput(File.DirRootExternal & "/Mein Haushaltsbuch", lFileName)
    
    Dim Datum_Aktuell, Zeit_Aktuell As String
    DateTime.DateFormat = "yyyy.MM.dd"
    DateTime.TimeFormat = "HH:mm:ss"


    Datum_Aktuell = DateTime.Date(DateTime.Now)' heutiges Datum als String
'    Zeit_Aktuell = DateTime.TimeParse(DateTime.Time(DateTime.Now))
    Zeit_Aktuell = DateTime.Time(DateTime.Now)
    
    
    
    Dim lFile As ExternalFile
    lFile=Storage.CreateNewFile(Storage.root,Datum_Aktuell.Trim & "_" & Zeit_Aktuell & "_" & lFileName)
    
    Dim outputstream1 As OutputStream
    outputstream1= Storage.OpenOutputstream(lFile)
    File.copy2(InputStream1,outputstream1)
    InputStream1.Close
    outputstream1.close
    
    
    'Zweite Datei sichern
    Dim lFileName As String="Daten_Mein_Haushaltsbuch.db-journal"
    Dim InputStream1 As InputStream
    InputStream1 = File.OpenInput(File.DirRootExternal & "/Mein Haushaltsbuch", lFileName)

    Dim lFile As ExternalFile
    lFile=Storage.CreateNewFile(Storage.root,Datum_Aktuell.Trim & "_" & Zeit_Aktuell & "_" & lFileName)

    Dim outputstream1 As OutputStream
    outputstream1= Storage.OpenOutputstream(lFile)
    File.copy2(InputStream1,outputstream1)
    InputStream1.Close
    outputstream1.close

    Msgbox2("Database copied. File Name: "&lFileName,"Info","OK","","",Null)
    
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
I would like to have the selection of the backup folder permanently stored in the program and not select it in the menu as now.
You can not prevent this. This is how Google designed this Feature. It is mandatory that the Deviceuser manually Select the root of the SDCard. You can save the patch after the user selects it for later use.
What is still missing is the creation of the backup folder, if it does not already exist.
This is part of the example i suggested in the past.

B4X:
    ' Get DocumentDir
    Dim docs As JavaObject = GetDocumentdDir(PersistantUri)
    
    ' Check if Destfolder Test already exists.
    Dim chkTest As ExternalFile = Storage.FindFile(Storage.Root,"Test")  ' Search for folder "Test"
    If chkTest.IsInitialized = False Then ' Folder does not exists
        'create the Folder "Test"
        docs.RunMethod("createDirectory",Array("Test")) ' create it
    End If
    ' Search the created Folder in the Storage Rootdir (we need to have a reference to it)
    Dim Test As ExternalFile = Storage.FindFile(Storage.Root,"Test") ' we now have a reference to the newly created folder.
    
    ' define destfile
    Dim destfile As ExternalFile = Storage.CreateNewFile(Test,filetocopy)
    ' Create an Outputstream to the destfile
    Dim os As OutputStream = Storage.OpenOutputStream(destfile)
    ' Create an Inputstream from the Sourcefile to copy
    Dim inpstr As InputStream  = File.OpenInput(File.Combine(File.DirRootExternal,"Download/"),filetocopy)
    ' Copy file
    File.Copy2(inpstr,os)
    ' Close Outputstream
    os.Close
 
Upvote 0

strupp01

Active Member
Licensed User
Longtime User
@DonManfred,
After your not so nice words in Post # 4, I think it's very good that you can help me further.
I never learned this programming, but by reading and trying it out I reached a certain level, but it also has big gaps.
The statement that I had to manually select the root was not known to me, because it was different earlier.
I will then also be able to create the backup folder. Because of the first problem, this couldn't be solved immediately.
Thanks again for your tireless help and greetings
strupp01
 
Upvote 0
Top