Android Question Import a csv file on android v9 device

anglia

Member
Licensed User
Longtime User
I need my app to import a csv file into a sqlite database.
The csv file is produced by an app on a PC and then copied to a folder on the android device.
If the device is android v9 sdk 28, how do I allow my app to access the csv file?

I know it's something to do with runtime permissions but I'm confused about how to go about it.

Any help greatly appreciated.
 

Mahares

Expert
Licensed User
Longtime User
Any help greatly appreciated.
If you save the database in File.DirInternal and the csv file in File.DirAssets you do not need to worry about runtimepermissions at all. But if you want to save the csv file and database file in FIle.DirRootExternal you need permission:

B4X:
Sub Globals
    Dim fname As String 'the file needs to be in the assets folder
End Sub

Sub Activity_Create(FirstTime As Boolean)
    fname = "countriespopulations2019.csv"
    Dim rp As RuntimePermissions   'need runtimepermissions lib cheched
    rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
    wait for Activity_PermissionResult(Permision As String, Result As Boolean)
    If Result = False Then
        MsgboxAsync("No permission to access external storage", "")
        Return
    Else    
        Log("granted permission")
        If File.Exists(File.DirRootExternal, fname) =False Then
            File.Copy(File.DirAssets,fname, File.DirRootExternal, fname)
            Log("file copied to dir external")
        End If    
    End If
    'Here you initialize the database and create it in file.DirRootExternal    
End Sub
You can of course import your data into the SQLite database directly from the csv even if it is located in File.DirAssets. This gives you an idea, but your best bet is to see the videos about rtp and the many hundreds threads about it. You may also be interested in saving the datbase in rp.GetSafeDirDefaultExternal
 
Upvote 0

anglia

Member
Licensed User
Longtime User
Many thanks Mahares,

It will be the user that copies/transfers the CSV file from a PC to the device and they obviously cannot store the CSV file in File.DirAssets.

My idea is that the user will upload the CSV file to cloud storage and will be accessible in the cloud folder on his/her device OR possibly email the CSV file to the android download folder.
I'm probably missing something very obvious, but what I don't see is how the user will be able to store the CSV file in a folder of their choice and be able to access and process the CSV file on an android device running SDK 28.

Is this possible?
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Is this possible?
The user can copy the csv file from his PC to the file.DirectoryExternal or its subfolder . The subfolder will be the one you program for him in your app if you want the files to be saved there instead of the root folder which is: File.DirRootExternal,. Then, via WIndows Explorer, he can see the file if you use a USB connection between device and PC. Usually, the folder is the same as the one you save the SQLite database to, but it does not have to be the same folder as the database folder. If the download folder of the CSV file is Download, you can still import its data into the database by referring to it as: File.DirRootExternal & "\Download\anglia.csv" without having to copy it anywhere else. As long as you have the correct path to the csv file, its data can be imported into the database that is saved either to file.DirectoryExternal or its subfolder. Remember , you still need the code I posted for runtimepermissions , but without the File.DirAssets part of the code.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…