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

Discussion in 'Android Questions' started by davepamn, Jul 1, 2015.

  1. davepamn

    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))
     
  2. NJDude

    NJDude Expert Licensed User

    Check THIS library.
     
  3. davepamn

    davepamn Active Member Licensed User

    Code:
    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: Jul 1, 2015
  4. Erel

    Erel Administrator Staff Member Licensed User

    The downloads folder path is:
    Code:
    File.Combine(File.DirRootExternal, "Download")
     
  5. davepamn

    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
     
  6. DonManfred

    DonManfred Expert Licensed User

    Then remember Erels answer

    so the resulting should be something like this i think (not tested; justed typed here in the forumeditor)

    Code:
    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
     
  7. davepamn

    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?
     
  8. DonManfred

    DonManfred Expert Licensed User

    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
    Code:
    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
     
  9. Erel

    Erel Administrator Staff Member Licensed User

    The permissions will be added automatically when you use File.DirRootExternal.
    Can you post the error message?
     
    DonManfred likes this.
Loading...