Android Question Select folder and save path

angel_

Well-Known Member
Licensed User
Longtime User
Currently the files (.txt) generated by the app are saved in a folder with:

B4X:
MyFolder = rs.GetSafeDirDefaultExternal("my_files")

I want the user to be able to change the "default" folder, is it possible to do this?, I tried to use SaveAs https://www.b4x.com/android/forum/t...-folder-list-of-other-related-methods.129897/, but it shows me the window to select the folder and save the file, and also I can't save the path of that new folder for then open the files directly without showingthe selection window
 

MicroDrie

Well-Known Member
Licensed User
Longtime User
The [B4X] TextEditor - Save and load external files example show how you can use an other location. However its hard to get the used directory location and used filename. Only with the Load function the full combined directory and file string can be grabbed with the help of the Apache StringUtils library and the following modification of the load function of the FilerHandler:

Updated Load function:
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)
    
'    --- https://www.b4x.com/android/forum/threads/90-string-manipulation-methods.73564/ ---
    Dim APSU As ApacheSU
    Dim FileNameStr As String = APSU.SubstringAfterLast(FileName, "%2F")
    Dim DirStr As String = APSU.SubstringBeforeLast(FileName, "%2F")
    Log(DirStr)
    Log(FileNameStr)
        
    Dim res As LoadResult = CreateLoadResult(Success, Dir, FileName)
    If res.Success Then ExtractInformationFromURI(res.FileName, res)
    Return res
End Sub

After changing the DirStr and FileNameStr to a global varaible you can test both values and skip the selection for the directory and filename.
 
Upvote 0

angel_

Well-Known Member
Licensed User
Longtime User
I think ApacheSU is only available for B4J and I needed it for B4A and B4i, I also preferred that it save the path when the user selects the folder or if this is not possible when using save as.
 
Upvote 0

MicroDrie

Well-Known Member
Licensed User
Longtime User
I think ApacheSU is only available for B4J and I needed it for B4A and B4i, I also preferred that it save the path when the user selects the folder or if this is not possible when using save as.
The B4A IDE indicates that it is a B4A library while the same jar file in B4J does not indicate any specific IDE ? solution. It therefore seems to be a Java library without a specific Android system call, so why not test it in the B4I IDE which I don't have. And if it doesn't work in B4I anyway, you can replicate the function I was too lazy for ? with the standard string utility of the IDE or with REGEX.
1648422741333.png
 
Upvote 0

angel_

Well-Known Member
Licensed User
Longtime User
I have tried ApacheStringUtils but it returns the same result as Log(FileName), file folder not showing.
 
Upvote 0

MicroDrie

Well-Known Member
Licensed User
Longtime User
I have tried ApacheStringUtils but it returns the same result as Log(FileName), file folder not showing.
1648451016557.png


My code doesn't change the original directory and filename strings, but simply use the new DirStr and FileNameStr variables. Even if I don't always understand it, everything has a reason. Undoubtedly also the at first sight illogical choice to provide the directory name variable with the filename and to keep the filename variable empty. Changing those variables could lead to unexpected problems so I (re)use my own variables with the suffix Str.
 
Upvote 0

angel_

Well-Known Member
Licensed User
Longtime User
I mean it has no effect using ApacheStringUtils and also I don't get the directory name either

Captura.JPG
 
Upvote 0

MicroDrie

Well-Known Member
Licensed User
Longtime User
You are absolutely right, but why ? are you logging the original variables ;) and not the result of the ApacheStringUtils ?? And why do you think I'm displaying the word Str in bold in my previous post ☺️? Precisely because we all read things incorrectly from time to time! o_O!

Let use know of ApacheStringUtils also works in B4i so that it can be used cross platform.
 
Upvote 0
Top