LAMW - SaveTextToUri to Downloads directory needs user action lazarus / 43-Android ---------------------------------------------------------------------- Alcatiz · 2024-09-25 07:01 Hi to all! I need to save a text file several times during execution, to the Downloads directory. I use the SaveTextToUri method. The file is correctly created and filled, but everytime the file is created I encounter two one problems: The user needs to press a "Save" button (see screenshot) or to choose a file to overwrite.There is no option to overwrite the file, so a copy with an incremental index is created (test.txt -> test (1).txt -> test (2).txt, a.s.o.). (solved saving URI to preferences as suggested in this topic)What can I do to correct this behaviour, so: No action is required by the user.File is overwritten. (solved)Many thanks!  My test code: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit unit1; {$mode delphi} interface uses  {$IFDEF UNIX}{$IFDEF UseCThreads}  cthreads,  {$ENDIF}{$ENDIF}  Classes, SysUtils, AndroidWidget, Laz_And_Controls, And_jni,  intentmanager, preferences;  type   { TAndroidModule1 }   TAndroidModule1 = class(jForm)    Button1: jButton;    EditText1: jEditText;    IntentManager1: jIntentManager;    Preferences1: jPreferences;    procedure AndroidModule1ActivityResult(Sender: TObject; ARequestCode: Integer; AResultCode: TAndroidResult; AIntentData: jObject);    procedure AndroidModule1JNIPrompt(Sender: TObject);    procedure Button1Click(Sender: TObject);  private   public    {public declarations}  end; var  AndroidModule1: TAndroidModule1; implementation  {$R *.lfm}   { TAndroidModule1 } procedure TAndroidModule1.Button1Click(Sender: TObject);(* Requests creation or update of the file *)begin  if Preferences1.GetStringData('uri', '') <> ''     then   (* URI saved to preferences: simply opens the file *)       begin         EditText1.Append('Calling RequestOpenFile' + LineEnding);         RequestOpenFile(GetEnvironmentDirectoryPath(dirDownLoads), 'text/plain', 112);       end     else   (* URI not saved yet to preferences: creates the file *)       begin         EditText1.Append('Calling RequestCreateFile' + LineEnding);         RequestCreateFile(GetEnvironmentDirectoryPath(dirDownLoads), 'text/plain', 'Test22', 111);       end;end; procedure TAndroidModule1.AndroidModule1ActivityResult(Sender: TObject; ARequestCode: Integer; AResultCode: TAndroidResult; AIntentData: jObject);(* Saves data to file *)var  LText: TStringList;  LUri: jObject;begin  if AResultCode = RESULT_OK     then       begin         if AIntentData = Nil            then              begin              EditText1.Append('   No data received, exit' + LineEnding);              Exit;              end;         LUri := IntentManager1.GetDataUri(AIntentData);         if LUri = Nil          then              begin                EditText1.Append('   URI NIL received, exit' + LineEnding);                Exit;              end;         case ARequestCode of           111: begin   (* Creates the file *)             EditText1.Append(   'Success! File created' + LineEnding);             Preferences1.SetStringData('uri', UriToString(LUri));   (* Saves URI to preferences so file won't be re- created *)             EditText1.Append(   'URI saved to preferences' + LineEnding);             LText := TStringList.Create;             try               LText.Append('Line 1');               LText.Append('Line 2');               SaveTextToUri(LText.Text, LUri);               EditText1.Append(   'Text written' + LineEnding);             finally               LText.Free;             end;           end;           112: begin   (* Updates the file *)           EditText1.Append(   'Success! File opened' + LineEnding);           LText := TStringList.Create;             try               LText.Append('Line 3');               LText.Append('Line 4');             SaveTextToUri(LText.Text, LUri);               EditText1.Append(   'Text written' + LineEnding);             finally               LText.Free;             end;           end;         end;       end;end; procedure TAndroidModule1.AndroidModule1JNIPrompt(Sender: TObject);(* Erases any URI from preferences *)begin  Preferences1.SetStringData('uri', '');end; end.