Android Question SaveCSV2 filename variable - What am I'm I missing

JThompson

New Member
Attempting to export to a CSV file. I have a dialog allowing the user to set the file name that is saved to a variable "fName." The app then checks for the extension ".csv."
If it is not there (or something else) it appends it.

B4X:
''//Remove Everything after '.' including the '.'  - Even if the user adds the ".csv"
        Dim DotPosition As Int
        DotPosition =  fName.IndexOf(".")
        If DotPosition>-1 Then
            'Dim NewName As String
            fName = fName.SubString2(0,DotPosition) & ".csv"
            Log(fName)
        Else
            fName = fName & ".csv"
        End If

Here is the code I use to get the correct directory:
B4X:
Dim rp As RuntimePermissions
        rp.GetSafeDirDefaultExternal("Download")
        Dim fileDir As String = File.DirRootExternal & "/Download"
        If File.IsDirectory(File.DirRootExternal , "Download") = False Then
            File.MakeDir(File.DirRootExternal, "Download")
        End If

I get the "java.io.FileNotFoundException: /storage/emulated/0/Download/FuelExpenses.csv: open failed: EACCES (Permission denied)" error while executing this line
B4X:
StrinUtil.SaveCSV2(fileDir, fName, ",", ListData,ListHeader)

If I leave off the ".csv" extension, no error.
If I hard code the name into the "SaveCSV2" file name, no error.
If I hard code the name into the variable fName (fName="Export.csv") then pass the variable to the SaveCSV2 function, no error.
It's only when I append the extension in code that I receive the error. Additionally I've tried to using the StringBuilder object to concatenate the two strings, same error.

My manifest does contain the 'android:requestLegacyExternalStorage="true"' attribute and I have tried setting the "targetSdkVersion" to 28, 31, and 33. Same error.

I'm a long-suffering VBA programmer and I'm new to B4A, so I know I'm missing something obvious.

Thanks in advance for any assistance.
 

JThompson

New Member
Thanks Erel. The code you sent works, but it defeats the purpose of what I'm attempting to do. I just want to force the ".csv" extension. With the SaveAs code I have no way of checking/changing the file name once the user hits the 'Save' button.
 
Upvote 0
Top