Android Question Writing to a simple text file

KarlB

Member
I am writing a data logger that appends a CSV string to a file every second. I must be able to open the file with Excel either on my cell phone or by sending it to my PC. Note that my phone is a Samsung S21 that does not support an SD card.

I have spent the last two days reading every forum and tutorial without gaining a single clue as to how to do this. I see criticism of using TextWriter but there is a comment by Erel saying that it is justified when appending lines to files. The TextEditor example works well but is far beyond my comprehension to extract the portion of the code that does what I want.

Here is what the data looks like:
2022-09-29 19-19-27,38.83687973022461,-77.47976684570312,39,0.20999999344348907,18.317604064941406,149.45,24.64,-0.17,2,-0.00,-0.06,24.66,0.400

Here is a snippet to save the data. This runs but I can't see the saved file. This comes from https://www.b4x.com/android/forum/threads/text-files.6690/

Sub getBTfullData
Private logData As String ' CSV from ESP32
Private payLoad() As String

txwBToutput.Write("D") ' request data from monitor
txwBToutput.Flush

Do While txrBTinput.Ready = False
Loop
logData = txrBTinput.ReadLine ' read a CSV string from BT
Log("Full Rcvd, " & logData)
payLoad = Regex.Split( ",", logData ) ' parse the CSV string into a field list
Private dataList As List
dataList.Initialize2( payLoad )
recID = dataList.Get(0)
Vbat = dataList.Get(1)
Ibat = dataList.Get(2)
Wbat = dataList.Get(3)
AH = dataList.Get(4)
WH = dataList.Get(5)
Vint = dataList.Get(6)
ESR = dataList.Get(7)

updateDataDisplay( dataList )

If runFlag = True Then
saveFile(logData)
End If
End Sub

Sub WriteTextWriter(fileName As String, data As String )
Dim TextWriter1 As TextWriter
Log("Write: " & data)
TextWriter1.Initialize(File.OpenOutput(File.DirInternal, fileName, True))
TextWriter1.WriteLine(data)
TextWriter1.Close
End Sub

Sub saveFile(data As String)
DateTime.DateFormat = "yyyy-MM-dd HH-mm-ss"
Dim str As String = ""
str = DateTime.Date( DateTime.Now ) & "," & latitude & "," & longitude & "," & altitude & "," & speed & "," & distance & "," & data & CRLF
WriteTextWriter(logFileName, str)
End Sub
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Please use [code]code here...[/code] tags when posting code.

There are several things here:
1. Never use TextReader with a network / Bluetooth stream. Only bad things will happen.
You should learn how to use AsyncStreams. In this case you need to use AsyncStreamsText

2. The writing code is fine.

File.DirInternal is a private folder. You can add an option to share the file: https://www.b4x.com/android/forum/threads/class-fileprovider-share-files.97865/#content
Or use the "SaveAs" code to let the user choose a folder: https://www.b4x.com/android/forum/threads/129897/#content
 
Upvote 0

AnandGupta

Expert
Licensed User
Longtime User
I use below to create text file, thanks to @LucaMs

See if it helps
 
Upvote 0

emexes

Expert
Licensed User
Here is what the data looks like:
2022-09-29 19-19-27,38.83687973022461,-77.47976684570312,39,0.20999999344348907,18.317604064941406,149.45,24.64,-0.17,2,-0.00,-0.06,24.66,0.400

This is a slight tangent, but: when you have time, consider running those four long decimal fractions through NumberFormat.

They seem to have been through a single-precision wringer, so anything after the ~7th significant digit is binary artifact noise anyway.

eg 0.20999999344348907 is presumably 0.21, which in single-precision is "rounded off" to the nearest binary equivalent 0.20999999344348907470703125 and then displayed to double-precision 17 decimal significant digits.

Longitude and latitude to 6 decimal places (after the decimal point) gives accuracy around 5 cm (= 2 inches in USA) cf consumer GPS accurate to 50 cm if you're lucky.

The 0.20999... looks like 3 or 4 decimal places would be plenty.

I don't know what the 18.317604064941406 is, but again: thanks to having been single-precision at some point, anything past the 7th significant digit is noise.
 
Upvote 0

KarlB

Member
This is a slight tangent, but: when you have time, consider running those four long decimal fractions through NumberFormat.

They seem to have been through a single-precision wringer, so anything after the ~7th significant digit is binary artifact noise anyway.

eg 0.20999999344348907 is presumably 0.21, which in single-precision is "rounded off" to the nearest binary equivalent 0.20999999344348907470703125 and then displayed to double-precision 17 decimal significant digits.

Longitude and latitude to 6 decimal places (after the decimal point) gives accuracy around 5 cm (= 2 inches in USA) cf consumer GPS accurate to 50 cm if you're lucky.

The 0.20999... looks like 3 or 4 decimal places would be plenty.

I don't know what the 18.317604064941406 is, but again: thanks to having been single-precision at some point, anything past the 7th significant digit is noise.
Thanks, good observations. If I can ever get the data to be saved in a useful manner, I will certainly do this clean up.
 
Upvote 0

KarlB

Member
I use below to create text file, thanks to @LucaMs

See if it helps
Can you explain how to use this with my code? I tried running your example and got many java error messages for line 35
B4X:
    File.Copy(File.DirAssets, mFileName, mOutDir, mFileName)
 
Upvote 0

KarlB

Member
Thanks, good observations. If I can ever get the data to be saved in a useful manner, I will certainly do this clean up.
The latest:
2022-09-30 18-27-21,38.8369,-77.4799,45,0,9.87,190.06,24.67,-0.11,0,-0.00,-0.02,24.72,-0.190

Sadly, I still can not write to a retrievable file storage location. I developed this app in MIT App Inventor but moved to B4A because it is much more professional. Now I'm finding only headaches.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

AnandGupta

Expert
Licensed User
Longtime User
Can you explain how to use this with my code? I tried running your example and got many java error messages for line 35
B4X:
    File.Copy(File.DirAssets, mFileName, mOutDir, mFileName)
Yes.
Copy from asset folder (inside apk) to default internal folder of the app in the phone (not external folder) xui.DefaultFolder
The file (mFileName) is "Test.txt".
 
Upvote 0
Top