Android Question How do I read a file in the download directory?

davepamn

Active Member
Licensed User
I want the user to read a file from download rather than having to move the file to external

Dim oReader as TextReader
oReader.Initialize(File.OpenInput(File.DirRootExternal,sFilename))
 

davepamn

Active Member
Licensed User
B4X:
Dim oFileDialog As FileDialog

    Dim sFileName As String=""

    If oFileDialog.Show("Select an xxx CSV","Select","","Close",Null)=-1Then

        sFileName=oFileDialog.ChosenName

        If sFileName<>"" Then

            LoadData(sFileName)

        Else

            Msgbox("Error loading data", "No File was selected")

            HideKeyboard

        EndIf



    EndIf
sub LoadData(sFilename as string)
dim myFile as publicfile

    oReader.Initialize(File.OpenInput(myFile.DirDownloads,sFilename))
end sub
Error Message: pen failed: ENOENT (No such file or directory)

I also tried passing the file path with the file name
sFileName=oFileDialog.FilePath & oFileDialog.ChosenName
Failed

The publicfile code seems only to apply to Save
 
Last edited:

davepamn

Active Member
Licensed User
Are you saying to get the name of the file than add the download path?

I am currently using file dialog to find the file in the download directory.

sFileName=oFileDialog.ChosenName
 

DonManfred

Expert
Licensed User
Are you saying to get the name of the file than add the download path?

I am currently using file dialog to find the file in the download directory.
Then remember Erels answer

The downloads folder path is:
B4X:
File.Combine(File.DirRootExternal, "Download")
so the resulting should be something like this i think (not tested; justed typed here in the forumeditor)

B4X:
Sub Button1_Click
    Dim oFileDialog As FileDialog
    Dim sFileName As String=""
    ' Set the Downloadpath as starting folder to be used in select...   
    oFileDialog.FilePath = File.Combine(File.DirRootExternal, "Download")
    If oFileDialog.Show("Select an xxx CSV","Select","","Close",Null) =-1 Then
        sFileName=oFileDialog.ChosenName
    If sFileName<>"" Then
            LoadData(File.Combine(File.DirRootExternal, "Download"),sFileName)
        Else
            Msgbox("Error loading data", "No File was selected")
    End If
    End If
End Sub

Sub LoadData(sPath As String, sFilename As String)
    'Dim myFile As publicfile
  oReader.Initialize(File.OpenInput(sPath,sFilename))
End Sub
 

davepamn

Active Member
Licensed User
I will try it.

The old code will work on some devices and not others. Do you think there is a security permission that prevents opening a file in the download directory?
 

DonManfred

Expert
Licensed User
Do you think there is a security permission that prevents opening a file in the download directory?
i dont know. But as File.DirRootExternal is considered as an external sdcard for android; i would try to set explizit permission to read/write external storage

Try to add one or both of
B4X:
AddPermission(android.permission.READ_EXTERNAL_STORAGE) ' Allows an application to read from external storage.
AddPermission(android.permission.WRITE_EXTERNAL_STORAGE) ' Allows an application to write to external storage.
to the manifest editor
 
Top