Recent

Author Topic: LAMW - SaveTextToUri to Downloads directory needs user action  (Read 249 times)

Alcatiz

  • New Member
  • *
  • Posts: 10
LAMW - SaveTextToUri to Downloads directory needs user action
« on: September 25, 2024, 09:01:34 am »
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][+][-]
  1. unit unit1;
  2.  
  3. {$mode delphi}
  4.  
  5. interface
  6.  
  7. uses
  8.   {$IFDEF UNIX}{$IFDEF UseCThreads}
  9.   cthreads,
  10.   {$ENDIF}{$ENDIF}
  11.   Classes, SysUtils, AndroidWidget, Laz_And_Controls, And_jni,  intentmanager, preferences;
  12.  
  13. type
  14.  
  15.   { TAndroidModule1 }
  16.  
  17.   TAndroidModule1 = class(jForm)
  18.     Button1: jButton;
  19.     EditText1: jEditText;
  20.     IntentManager1: jIntentManager;
  21.     Preferences1: jPreferences;
  22.     procedure AndroidModule1ActivityResult(Sender: TObject; ARequestCode: Integer; AResultCode: TAndroidResult; AIntentData: jObject);
  23.     procedure AndroidModule1JNIPrompt(Sender: TObject);
  24.     procedure Button1Click(Sender: TObject);
  25.   private
  26.  
  27.   public
  28.     {public declarations}
  29.   end;
  30.  
  31. var
  32.   AndroidModule1: TAndroidModule1;
  33.  
  34. implementation
  35.  
  36. {$R *.lfm}
  37.  
  38.  
  39. { TAndroidModule1 }
  40.  
  41. procedure TAndroidModule1.Button1Click(Sender: TObject);
  42. (* Requests creation or update of the file *)
  43. begin
  44.   if Preferences1.GetStringData('uri', '') <> ''
  45.      then   (* URI saved to preferences: simply opens the file *)
  46.        begin
  47.          EditText1.Append('Calling RequestOpenFile' + LineEnding);
  48.          RequestOpenFile(GetEnvironmentDirectoryPath(dirDownLoads), 'text/plain', 112);
  49.        end
  50.      else   (* URI not saved yet to preferences: creates the file *)
  51.        begin
  52.          EditText1.Append('Calling RequestCreateFile' + LineEnding);
  53.          RequestCreateFile(GetEnvironmentDirectoryPath(dirDownLoads), 'text/plain', 'Test22', 111);
  54.        end;
  55. end;
  56.  
  57. procedure TAndroidModule1.AndroidModule1ActivityResult(Sender: TObject; ARequestCode: Integer; AResultCode: TAndroidResult; AIntentData: jObject);
  58. (* Saves data to file *)
  59. var
  60.   LText: TStringList;
  61.   LUri: jObject;
  62. begin
  63.   if AResultCode = RESULT_OK
  64.      then
  65.        begin
  66.          if AIntentData = Nil
  67.             then
  68.               begin
  69.                 EditText1.Append('   No data received, exit' + LineEnding);
  70.                 Exit;
  71.               end;
  72.          LUri := IntentManager1.GetDataUri(AIntentData);
  73.          if LUri = Nil
  74.             then
  75.               begin
  76.                 EditText1.Append('   URI NIL received, exit' + LineEnding);
  77.                 Exit;
  78.               end;
  79.          case ARequestCode of
  80.            111: begin   (* Creates the file *)
  81.              EditText1.Append(   'Success! File created' + LineEnding);
  82.              Preferences1.SetStringData('uri', UriToString(LUri));   (* Saves URI to preferences so file won't be re-created *)
  83.              EditText1.Append(   'URI saved to preferences' + LineEnding);
  84.              LText := TStringList.Create;
  85.              try
  86.                LText.Append('Line 1');
  87.                LText.Append('Line 2');
  88.                SaveTextToUri(LText.Text, LUri);
  89.                EditText1.Append(   'Text written' + LineEnding);
  90.              finally
  91.                LText.Free;
  92.              end;
  93.            end;
  94.            112: begin   (* Updates the file *)
  95.              EditText1.Append(   'Success! File opened' + LineEnding);
  96.              LText := TStringList.Create;
  97.              try
  98.                LText.Append('Line 3');
  99.                LText.Append('Line 4');
  100.                SaveTextToUri(LText.Text, LUri);
  101.                EditText1.Append(   'Text written' + LineEnding);
  102.              finally
  103.                LText.Free;
  104.              end;
  105.            end;
  106.          end;
  107.        end;
  108. end;
  109.  
  110. procedure TAndroidModule1.AndroidModule1JNIPrompt(Sender: TObject);
  111. (* Erases any URI from preferences *)
  112. begin
  113.   Preferences1.SetStringData('uri', '');
  114. end;
  115.  
  116. end.
« Last Edit: September 25, 2024, 10:11:05 am by Alcatiz »

 

TinyPortal © 2005-2018