Android Question Writing text strings and Lists to file

Angel Maza

Member
Licensed User
Longtime User
I tried to implement Erel's tutorial ( https://www.b4x.com/android/forum/threads/text-files.6690/ ), but I can't write to a file.

Erel states that the directories will be created as needed. However, I get a 'java.io.FileNotFoundException: /storage/emulated/0/Android/data/com.calypsoinstruments.CalypsoUltrasonicAPI/files/CompassCalibrationData.txt (No such file or directory)' Exception.

Any suggestions?

B4a code & Manifest entries:
B4X:
    Dim fileName As String = "CompassCalibrationData.txt"
    Dim list1 As List
    list1.Initialize
    list1.Add(1.1)
    list1.Add(2.2)
    list1.Add(3.3)
    
    If File.ExternalWritable = False Then
        Msgbox2("Cannot write to the default storage location. Can not start the calibration process", "Storage Access Error", "OK", "", "", Starter.appIcon)
        Return
    End If

    Log("file exits: " & File.Exists(File.DirDefaultExternal, fileName))
    If File.Exists(File.DirDefaultExternal, fileName) = False Then
        File.WriteString(File.DirDefaultExternal, fileName, "test string to file")
        Log("test written to: " & File.DirDefaultExternal & "/" & fileName)
        
        File.WriteList(File.DirDefaultExternal, fileName, list1)
    End If

Manifest entries:
' Permissions
AddPermission(android.permission.ACCESS_NETWORK_STATE)
AddPermission(android.permission.ACCESS_COARSE_LOCATION) 'no necesaria si no BluetoothAdmin
AddPermission(android.permission.ACCESS_FINE_LOCATION)
AddPermission(android.permission.BLUETOOTH)
AddPermission(android.permission.BLUETOOTH_ADMIN)
AddPermission(android.permission.EXTERNAL_STORAGE)
AddPermission(android.permission.RECEIVE_ULTRASONIC_API)

Error log:
B4X:
Initialize: alpha = 0.25  smoothing: 0.25
Main->Launch_Background_Services(): pref Mac: C7:59:53:20:73:F9
Starter->after magDeclination() : localDeclination = 0.06210149824619293
file exits: false
Error occurred on line: 420 (Main)  -> File.WriteString(File.DirDefaultExternal, fileName, "test string to file")
java.io.FileNotFoundException: /storage/emulated/0/Android/data/com.calypsoinstruments.CalypsoUltrasonicAPI/files/CompassCalibrationData.txt (No such file or directory)
    at java.io.FileOutputStream.open(Native Method)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
    at anywheresoftware.b4a.objects.streams.File.OpenOutput(File.java:448)
    at anywheresoftware.b4a.objects.streams.File.WriteString(File.java:263)
    at com.calypsoinstruments.CalypsoUltrasonicAPI.main._calibration_click(main.java:1312)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:735)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:360)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:260)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at anywheresoftware.b4a.BA$1.run(BA.java:330)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6123)
    at java.lang.reflect.Method.invoke(Native Method)
 

Angel Maza

Member
Licensed User
Longtime User
adding the Run-time permission request:

Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
Wait For Activity_PermissionResult(Permission As String, Result As Boolean)
If Result Then
...
all the file writing code here
....
End If

solved the problem.

Thanks
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…