Bug: app.WriteFile() does not truncate SAF files

56 views
Skip to first unread message

Ferhat

unread,
Aug 5, 2026, 2:04:59 AM (4 days ago) Aug 5
to DroidScript
Bug: app.WriteFile() doesn't truncate SAF files

1) Steps to reproduce:
Select a text file with app.ChooseFile().

2) Write a long string:
Hello how are you?

3) Write a shorter string:
I'm good.

4) Read the file using app.ReadFile().
Expected: The file should contain only:
I'm good.

Actual: The file is not truncated correctly. Writing longer content works, but writing shorter content leaves the previous content instead.


Example:

//Called when application is started.
function OnStart()
{
//Create a layout with objects vertically centered.
lay = app.CreateLayout( "Linear", "VCenter,FillXY" )

  app.ChooseFile( "Choose a file", "*/*", test )
 
  txt = app.CreateText( "" )
  lay.AddChild( txt )

//Create a text label and add it to layout.
btnshort = app.CreateButton( "Long" )
btnshort.SetOnTouch( btnshort_OnTouch )
lay.AddChild( btnshort )

btnlong = app.CreateButton( "Short" )
btnlong.SetOnTouch( btnlong_OnTouch )
lay.AddChild( btnlong )

//Add layout to app.
app.AddLayout( lay )
}

function test( file )
{
  fileuri = file;
txt.SetText( app.ReadFile( file ) )
}

function btnshort_OnTouch()
{
app.WriteFile( fileuri, "Hello how are you?" )
txt.SetText( app.ReadFile( fileuri ) )
}

function btnlong_OnTouch()
{
app.WriteFile( fileuri, "I'm good." )
  txt.SetText( app.ReadFile( fileuri ) )
}

Alan Hendry

unread,
Aug 5, 2026, 11:58:24 AM (3 days ago) Aug 5
to DroidScript
HI, 
I can confirm that the file wrongly contains
I'm good. are you?
Even if long text is written and app is closed
then app rerun and short text written
same erroneous result.
Choosefile returns a uri, does this happen for path?
If you are trying to workaround you could try
CreateFile
(options are r/w/rw, methods WriteText/Close)
Regards, ah
 

Ferhat

unread,
Aug 5, 2026, 1:07:13 PM (3 days ago) Aug 5
to DroidScript

Hi,

Thank you for your reply and for confirming the issue.

The problem only occurs with files accessed through the Storage Access Framework (SAF), including files selected with ChooseFile() and files inside folders that the user has granted access to.

I haven't tested normal file paths yet, but I tested files created in app.GetPrivateFolder(), and the issue does not occur there.

So the problem appears to be specific to SAF file access.


5 Ağustos 2026 Çarşamba tarihinde saat 18:58:24 UTC+3 itibarıyla hendr...@gmail.com şunları yazdı:
Message has been deleted

Ferhat

unread,
Aug 5, 2026, 2:50:02 PM (3 days ago) Aug 5
to DroidScript
Hi,

Thank you for the suggestion.

I tested "CreateFile()", but it returns "null" for files selected with "ChooseFile()" and also for files inside folders that the user has granted access to through SAF.

It seems that "CreateFile()" cannot open SAF files or folders, so unfortunately I can't use it as a workaround.

5 Ağustos 2026 Çarşamba tarihinde saat 20:07:13 UTC+3 itibarıyla Ferhat şunları yazdı:
Reply all
Reply to author
Forward
0 new messages