Android Question DBUtils... My database added to file assets seems not to be there

FERNANDO SILVEIRA

Active Member
Licensed User
Hello guys,

I have a database (genealogy.db) that I'm trying to read and added it to my app assets using Designer tool files tab, however when I run the program I got the following message when during File.Copy(File.DirAssets, FileName, TargetDir, FileName):

Logger connected to: samsung SM-J500M
--------- beginning of system
--------- beginning of main
Copying updated assets files (1)
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Error occurred on line: 32 (DBUtils)
java.io.FileNotFoundException: /data/user/0/b4a.GenealogyLite/files/virtual_assets/genealogy.db: open failed: ENOENT (No such file or directory)

B4X:
Sub CopyDBFromAssets (FileName As String) As String
    Dim TargetDir As String
    If File.ExternalWritable Then
        TargetDir = File.DirDefaultExternal
    Else
        TargetDir = File.DirInternal
    End If
    If File.Exists(TargetDir, FileName) = False Then
        File.Copy(File.DirAssets, FileName, TargetDir, FileName)
    End If
    Return TargetDir
End Sub

I then check the files tab of the Designer and the file is not there anymore, even file been still in PC asset folder.

Any clues on where should I put my database file?
 

DonManfred

Expert
Licensed User
Longtime User
You dont need to add it to the designers files
But you should sync the file folder from the ide itself
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Not sure I got you... :confused:
The FILES TAB in the IDE itself. NOT the files tab in the Designer. The Tab in designer you need to add IMAGES you want to use in the Design.

Open the files tab (again; the one from the IDE (beside library tab, modules tab, ...), click SYNC
 
Upvote 0

FERNANDO SILVEIRA

Active Member
Licensed User
The FILES TAB in the IDE itself. NOT the files tab in the Designer. The Tab in designer you need to add IMAGES you want to use in the Design.

Open the files tab (again; the one from the IDE (beside library tab, modules tab, ...), click SYNC
You mean the IDE's FILES MANAGER window ?

Ok, added genealogy.db there, but the error is just the same.

upload_2018-5-18_14-23-13.png
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
added genealogy.db there, but the error is just the same.
If your TargetSDK is 26, you probably need runtime Permissions. If that is the case then your code should be like this:
B4X:
Private rp As RuntimePermissions  'in globals

CopyDBFromAssets (“genealogy.db”) 'in Activity Create

Sub CopyDBFromAssets (FileName As String) As String
    Dim TargetDir As String
    If File.ExternalWritable Then
        TargetDir = rp.GetSafeDirDefaultExternal("")   '<----- mahares
    Else
        TargetDir = File.DirInternal
    End If
    If File.Exists(TargetDir, FileName) = False Then
        File.Copy(File.DirAssets, FileName, TargetDir, FileName)
    End If
    Return TargetDir
End Sub
 
Upvote 0

FERNANDO SILVEIRA

Active Member
Licensed User
If your TargetSDK is 26, you probably need runtime Permissions. If that is the case then your code should be like this:
B4X:
Private rp As RuntimePermissions  'in globals

CopyDBFromAssets (“genealogy.db”) 'in Activity Create

Sub CopyDBFromAssets (FileName As String) As String
    Dim TargetDir As String
    If File.ExternalWritable Then
        TargetDir = rp.GetSafeDirDefaultExternal("")   '<----- mahares
    Else
        TargetDir = File.DirInternal
    End If
    If File.Exists(TargetDir, FileName) = False Then
        File.Copy(File.DirAssets, FileName, TargetDir, FileName)
    End If
    Return TargetDir
End Sub
Thank you for your effort, Mahares.

Yes, TargetSDK is 26.
I applied your changes and declared Private rp As RuntimePermissions in Main.Globals, but I got sintax error undeffined variable rp.
I then tried to declare rp in Main.Process_Globals and got the same undeffined variable rp.

Finally I put rp on DBUTILS.Process_Globals and got no sintax error, however when run app the initial Error occurred on line: 33 (DBUtils)
java.io.FileNotFoundException: /data/user/0/b4a.GenealogyLite/files/virtual_assets/genealogy.db: open failed: ENOENT (No such file or directory)
still persists.

I even checked upper/lower case genealogy.db versus Genealogy.db in DBFileName = "genealogy.db" and the result is the same.
 

Attachments

  • APP09 Genealogy.ZIP
    18.7 KB · Views: 404
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Here is what you need to do Fernando to get it to work:
In main activity_Create, see the line I added :
B4X:
Sub Activity_Create(FirstTime As Boolean)
    DBFileDir= DBUtils.rp.GetSafeDirDefaultExternal("")  'mahares

    If FirstTime Then
        DBFileDir = DBUtils.CopyDBFromAssets(DBFileName)
    End If
    SQL1.Initialize(DBFileDir, DBFileName, True)

and in DBUtils module change:
B4X:
Private rp As RuntimePermissions
to:
B4X:
Public rp As RuntimePermissions

and change this below sub back to:
B4X:
Sub CopyDBFromAssets (FileName As String) As String
    Dim TargetDir As String
    If File.ExternalWritable Then
        TargetDir = File.DirDefaultExternal  '<----- mahares
    Else
        TargetDir = File.DirInternal
    End If  
    If File.Exists(TargetDir, FileName) = False Then
        File.Copy(File.DirAssets, FileName, TargetDir, FileName)
    End If
    Return TargetDir
End Sub
 
Upvote 0

FERNANDO SILVEIRA

Active Member
Licensed User
Here is what you need to do Fernando to get it to work:
In main activity_Create, see the line I added :
B4X:
Sub Activity_Create(FirstTime As Boolean)
    DBFileDir= DBUtils.rp.GetSafeDirDefaultExternal("")  'mahares

    If FirstTime Then
        DBFileDir = DBUtils.CopyDBFromAssets(DBFileName)
    End If
    SQL1.Initialize(DBFileDir, DBFileName, True)

and in DBUtils module change:
B4X:
Private rp As RuntimePermissions
to:
B4X:
Public rp As RuntimePermissions

and change this below sub back to:
B4X:
Sub CopyDBFromAssets (FileName As String) As String
    Dim TargetDir As String
    If File.ExternalWritable Then
        TargetDir = File.DirDefaultExternal  '<----- mahares
    Else
        TargetDir = File.DirInternal
    End If 
    If File.Exists(TargetDir, FileName) = False Then
        File.Copy(File.DirAssets, FileName, TargetDir, FileName)
    End If
    Return TargetDir
End Sub

Man, you made it!!!
Finally we passed that point. :):D:rolleyes:
Thank you very much.

That is the file on the device:
upload_2018-5-18_20-29-2.png
 
Upvote 0
Top