Android Question How to write to file in File.DirRootExternal in Android 33+

Markos

Active Member
Licensed User
Longtime User
Hi All,

I have 3 questions:

1) I
require functionality to edit a file , make edits and save to same file,, do I have the ability to overwrite files in DirRootExternal , if not then I must create a new file to save that new version?

2) What is the procedure to write to any file type in File.DirRootExternal I used to use RandomAccessFile but now I use ContentResolver and File.Readbytes to get a byte array to read from a file that works fine.

Now I assumed if the following code worked for Reading
B4X:
        Dim cc As ContentChooser
        cc.Initialize("cc")
        cc.Show("*/*", "Choose File")
        Wait For CC_Result (Success As Boolean, dirtmp As String, filenametmp As String)
        Dim res As LoadResult = CreateLoadResult(Success, dirtmp, filenametmp)
        If res.Success Then
        res.RealName=GetFileInfoByIndex("_display_name", res.filename)
        sa=File.ReadBytes(res.Dir,res.filename)

I thought I could use the following Writing to same file using the byte array sa
B4X:
File.WriteBytes(res.Dir,res.filename,sa)

But it returns an error saying cannot write to file.

3) Is there a way to convert RandomAccessFile to an Inputstream?
 
Last edited:

Markos

Active Member
Licensed User
Longtime User
Your options are available here: SaveAs - Let the user select a target folder + list of other related methods

You cannot directly write to File.DirRootExternal.
Hi Erel,

Thanks! that feedback about DirRootExternal is invaluable. I opted to save the edited file to DirDefautlExternal and it saved successfully

1) Can I share the file in DirDefautlExternal to allow another device to access or copy to another device , what methods exists ? Only ftp, if so how to do this
2) Devices like new Galaxy phones don't have sd slots will DirDefautlExternal still be relevant
 
Upvote 0
Top