Android Question B4X Load .csv file from download folder

MList

Member
Licensed User
Hi,
i would like to import and export a .csv file (build from B4xtabledata) directly to the Download Folder.
With su.save i can't put it directly to the download folder, so i did SaveAs afterwards.
So the user has to save the file 2 times, he doesn't know why. Is there any other possibility?
The same with import, i want to import directly from download folder..
var.filepath = xui.defaultfolder
Perhaps i didn't get it right 🙈
Thanks a lot for help
Marion



B4X:
 '     Export .csv
            su.SaveCSV2(Var.Filepath,fd.ChosenName, ";", data, headers)

            Wait For (B4XPages.MainPage.SaveAs(File.OpenInput(fd.FilePath, fd.ChosenName), "application/octet-stream", fd.ChosenName)) Complete (Success As Boolean)
            Log("File saved successfully? " & Success)

' Import
            data = su.LoadCSV(Var.FilePath,fd.ChosenName,";")
 

Mahares

Expert
Licensed User
Here is how I would do it to save the file only once, although there may be other simpler and better ways:
  1. You have a list called Data that you used to populate the B4XTable or created using a CreateDataView.
  2. Convert the list ‘Data’ to a string using StringBuilder by iterating through each list line. If you end each line with CRLF, you may have to remove the final CRLF
  3. Add to you B4XTable project code, the code from the project in the TextEditor whose link is:
https://www.b4x.com/android/forum/t...-save-and-load-external-files.132731/#content

Don’t forget to check all the needed libraries used in the textEditor like: ContentChooser and FileProvider.
  1. You load the layout that comes with the TextEditor. It has a B4XFloatEditText called: txtField You can then display the text of the list you just converted to string by populating the B4XFLoatTextField with something like this:
txtField.Text= sb.ToString.SubString2(0, sb.ToString.Length-1)
  1. Use the upper right corner Save menu button from the TextEditor code to save the file to the download folder of your device by selecting it from the listed folders.: Here is the sub that saves it:
B4X:
Private Sub Save
#if B4A
Dim b() As Byte = txtField.Text.GetBytes("UTF8")
Dim in As InputStream
in.InitializeFromBytesArray(b, 0, b.Length)
Wait For (FileHandler1.SaveAs(in, "text/plain", "texteditorsample")) Complete (Success As Boolean) 'it adds extension: txt to file
#else if B4i
Wait For (FileHandler1.SaveAs(Me, B4XPages.GetNativeParent(Me).TopRightButtons.Get(0), txtField.Text)) Complete (Success As Boolean)
#end if
If Success Then
    toast.Show("File saved successfully")
Else
    oast.Show("File not saved")
End If
End Sub
 
Upvote 1
Top