Android Question load to application start application .csv file

fgh3966

Active Member
Licensed User
Hello Everybody

I want to load a file for example csv file, that are into the directory Téléchargement and use it as if it had been supported by this sub.

B4X:
Private Sub HandleLoadResult(Result As LoadResult)
    If Result.Success Then
        Try
            Dim Reader As TextReader
            Reader.Initialize(File.OpenInput(Result.Dir, Result.FileName))
'             Dim line As String
            line = Reader.ReadLine
'            stringList.Add(line)
            Do While line <> Null
                line = Reader.ReadLine
                stringList.Add(line)
                increm = increm +1        ' nombre lignes dans le CSV
            Loop
            Reader.Close
    '        chainetexte = File.ReadString(Result.Dir, Result.FileName)
            toast.Show($"File '${Result.RealName}' loaded"$)
        Catch
            chainetexte = "Error loading file"
            'Log(LastException)
        End Try
    End If
'    LoadGTR2
    Log ("nbre lignes  "&increm)
'    txtField.Text = chainetexte   
End Sub

It is possible ?

Thanks.
 

Attachments

  • csvBase.zip
    13.8 KB · Views: 33

fgh3966

Active Member
Licensed User
Hello all

Excuse my bad English

The process around Reader is difficult to understand.
When starting application, Reader should load with a csv file and the file name and path would be indicated by variables.
I think that i need two variables : One for the path and another for the file name.

I wrote the code below: it is executed but Reader did not load the csv file

B4X:
Private Sub HandleGO '(Result) ' As LoadResult)
Dim ffile, ppath As String
ppath = "Download"
ffile = "file2023.csv"

        Try
            Dim Reader As TextReader
            Reader.Initialize(File.OpenInput(ppath, ffile))
            
            etc ...

End Sub

Any help please ?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

fgh3966

Active Member
Licensed User
in red color debugger say >>> Caused by: java.io.FileNotFoundException: Download/file2023.csv (No such file or directory)
It's android 8.01
How to know path into android with b4a ? I think the ls -l command is not working?
 
Upvote 0

teddybear

Well-Known Member
Licensed User
in red color debugger say >>> Caused by: java.io.FileNotFoundException: Download/file2023.csv (No such file or directory)
It's android 8.01
How to know path into android with b4a ? I think the ls -l command is not working?
Why do you use the Download directory? do you know its absolute path? where is the file2023.csv from?
 
Upvote 0

fgh3966

Active Member
Licensed User
I use the Download folder because it seems simple for users, maybe the documents folder?
If you recommend other places I'm willing to try.
I would like to code an app that is easy to use and if possible not too complicated to program .

file2023.csv are into download directory
 
Upvote 0

teddybear

Well-Known Member
Licensed User
I use the Download folder because it seems simple for users, maybe the documents folder?
If you recommend other places I'm willing to try.
I would like to code an app that is easy to use and if possible not too complicated to program .

file2023.csv are into download directory
Where is the file2023.csv from, third App? see your post#1, you are using contentchooser to get file, the directory is contentDir
 
Last edited:
Upvote 0

fgh3966

Active Member
Licensed User
I use using contentchooser to get file because this function are coded into an example (I don't really understand how it works)
New i want to load a csv file into Reader object or variable by specifying the path and name of the csv file

file2023.csv file are into Download directory
 
Upvote 0

teddybear

Well-Known Member
Licensed User
both are available in the ContentChooser Result event.

B4X:
    Wait For CC_Result (Success As Boolean, Dir As String, FileName As String)

You can use Dir and Filename in this Event
The example in post #1 it can read text file using CC, I don't know why to read file2023.csv by specified Download path in Post #3? so I asked where the file2023.csv is from.
 
Upvote 0

fgh3966

Active Member
Licensed User
Into filehandler i copy and rename Load As ResumableSub and disable cc.Show("text/*", "Choose text file")

B4X:
Public Sub StartLoad As ResumableSub
    Dim cc As ContentChooser
    cc.Initialize("cc")
'    cc.Show("text/*", "Choose text file")
    Wait For CC_Result (Success As Boolean, Dir As String, FileName As String)
    Dim res As LoadResult = CreateLoadResult(Success, Dir, FileName)
    If res.Success Then ExtractInformationFromURI(res.FileName, res)
    Return res
End Sub

Public Sub Load As ResumableSub
    Dim cc As ContentChooser
    cc.Initialize("cc")
    cc.Show("text/*", "Choose text file")
    Wait For CC_Result (Success As Boolean, Dir As String, FileName As String)
    Dim res As LoadResult = CreateLoadResult(Success, Dir, FileName)
    If res.Success Then ExtractInformationFromURI(res.FileName, res)
    Return res
End Sub

and into B4Xmainpage i copy btnload_clik sub

B4X:
Private Sub BtnLoad_Click 
    #if B4A
    Wait For (FileHandler1.Load) Complete (Result As LoadResult)
    #else if B4i
    Wait For (FileHandler1.Load(Me, B4XPages.GetNativeParent(Me).TopRightButtons.Get(1))) Complete (Result As LoadResult)
    #end if
    'txtField.textfield.enabled = True
    HandleLoadResult(Result)
End Sub

private Sub StartBtnLoad 
    #if B4A
    Wait For (FileHandler1.StartLoad) Complete (Result As LoadResult)
    #else if B4i
    Wait For (FileHandler1.Load(Me, B4XPages.GetNativeParent(Me).TopRightButtons.Get(1))) Complete (Result As LoadResult)
    #end if
    'txtField.textfield.enabled = True
    HandleLoadResult(Result)
End Sub

Now I have to pass variables between sub and classes.
 
Last edited:
Upvote 0

teddybear

Well-Known Member
Licensed User
Into filehandler i copy and rename Load As ResumableSub and disable cc.Show("text/*", "Choose text file")

B4X:
Public Sub StartLoad As ResumableSub
    Dim cc As ContentChooser
    cc.Initialize("cc")
'    cc.Show("text/*", "Choose text file")
    Wait For CC_Result (Success As Boolean, Dir As String, FileName As String)
    Dim res As LoadResult = CreateLoadResult(Success, Dir, FileName)
    If res.Success Then ExtractInformationFromURI(res.FileName, res)
    Return res
End Sub

Public Sub Load As ResumableSub
    Dim cc As ContentChooser
    cc.Initialize("cc")
    cc.Show("text/*", "Choose text file")
    Wait For CC_Result (Success As Boolean, Dir As String, FileName As String)
    Dim res As LoadResult = CreateLoadResult(Success, Dir, FileName)
    If res.Success Then ExtractInformationFromURI(res.FileName, res)
    Return res
End Sub

and into B4Xmainpage i copy btnload_clik sub

B4X:
Private Sub BtnLoad_Click
    #if B4A
    Wait For (FileHandler1.Load) Complete (Result As LoadResult)
    #else if B4i
    Wait For (FileHandler1.Load(Me, B4XPages.GetNativeParent(Me).TopRightButtons.Get(1))) Complete (Result As LoadResult)
    #end if
    'txtField.textfield.enabled = True
    HandleLoadResult(Result)
End Sub

private Sub StartBtnLoad
    #if B4A
    Wait For (FileHandler1.StartLoad) Complete (Result As LoadResult)
    #else if B4i
    Wait For (FileHandler1.Load(Me, B4XPages.GetNativeParent(Me).TopRightButtons.Get(1))) Complete (Result As LoadResult)
    #end if
    'txtField.textfield.enabled = True
    HandleLoadResult(Result)
End Sub

Now I have to pass variables between sub and classes.
You disabled the cc.Show, CC_Result event would not be fired.
 
Upvote 0

fgh3966

Active Member
Licensed User
Hello
Thank Teddybear, but when cc.Show("text/*", "Choose text file") are enabled, file2023.csv are just visible in a windows and not loaded.

Can be integrate the following code in filehandler sub ?


B4X:
Sub loaddefault(result As LoadResult)
    Dim su As StringUtils

    result = su.LoadCSV(File.DirInternal & "/documents", "file2023x.csv", ",")
   
'    Result = su.LoadCSV(File.DirInternal & "/Téléchargements"file2023x.csv", ",")
    HandleLoadResult(result)
End Sub
 
Upvote 0

fgh3966

Active Member
Licensed User
Hi
I want compile Gallery Chooser.zip to understand path, browsing androïd, etc ... however iJK_MaterialToast library is missing, where can I find it please because I don't have telegram.

Thanks
 
Upvote 0

teddybear

Well-Known Member
Licensed User
Hello
Thank Teddybear, but when cc.Show("text/*", "Choose text file") are enabled, file2023.csv are just visible in a windows and not loaded.

Can be integrate the following code in filehandler sub ?


B4X:
Sub loaddefault(result As LoadResult)
    Dim su As StringUtils

    result = su.LoadCSV(File.DirInternal & "/documents", "file2023x.csv", ",")
  
'    Result = su.LoadCSV(File.DirInternal & "/Téléchargements"file2023x.csv", ",")
    HandleLoadResult(result)
End Sub

I really don't quite understand where file2023x.csv is from, is it shared by third app or does your app download?

Hi
I want compile Gallery Chooser.zip to understand path, browsing androïd, etc ... however iJK_MaterialToast library is missing, where can I find it please because I don't have telegram.

Thanks
Regarding this issue you'd better create a new thread to ask.
 
Upvote 0

fgh3966

Active Member
Licensed User
Hello all

I try this code because old code had many problems

B4X:
into handleloadresult
public Sub loaddefault As ResumableSub
    Log ("essai")
    Dim su As StringUtils
    Dim res As LoadResult ' = CreateLoadResult(Success, Dir, FileName)
    res = su.LoadCSV(File.DirInternal & "", "file2023x.csv", ",")
    If res.Success Then ExtractInformationFromURI(res.FileName, res)
    Return res   
End Sub

in B4Xmainpage i call it with
B4X:
Private Sub Loaddefault
    #if B4A
    Wait For (FileHandler1.Loaddefault) Complete (Result As LoadResult)
    #else if B4i
    Wait For (FileHandler1.Load(Me, B4XPages.GetNativeParent(Me).TopRightButtons.Get(1))) Complete (Result As LoadResult)
    #end if
    HandleLoadResult(Result)
End Sub


I did several tests and obtain : java.io.FileNotFoundException: /data/user/0/b4a.example/files/file2023x.csv (No such file or directory)
'log are correct)
Of course I manually copied file2023x.csv into the /files directory
Into my phone android 8.01 i don't see part of folder /data/user/0/
Maybe android created these directories?
What do you think?
Thanks in advance.
 
Upvote 0
Top