Recent

Author Topic: Write to txt file on android LAMW  (Read 12844 times)

Xander

  • New Member
  • *
  • Posts: 14
Write to txt file on android LAMW
« on: June 25, 2017, 09:27:22 am »
Im trying to write to a txt file on android. When I used delphi I was able to do this but in Lazarus the same way does not work. Laz4Android does not recognise IOUtils in the uses so I cant use my applications home path.
Code: Pascal  [Select][+][-]
  1. uses
  2.   Classes, SysUtils, AndroidWidget, Laz_And_Controls, textfilemanager,
  3.   opendialog, fileprovider, IOUtils ;
  4.  
  5.  
  6.   public
  7.     sFilePath : String;
  8.     txtFile : TextFile;
  9.  
  10.     sString : String;
  11.  
  12.     const
  13.       AppPath = TPath.GetHomePath;
  14.  
  15. //Write to file
  16.  
  17.   sFilePath := TPath.Combine(AppPath, 'Day 1.txt');
  18.   AssignFile(txtFile, sFilePath);
  19.  
  20.   if FileExists(sFilePath) then
  21.   begin
  22.     ShowMessage('True');
  23.     Append(txtFile);
  24.     WriteLn(txtFile,sString);
  25.     CloseFile(txtFile);
  26.   end else
  27.   begin
  28.     ShowMessage('False');
  29.     Rewrite(txtFile);
  30.     WriteLn(txtFile,sString);
  31.     CloseFile(txtFile);
  32.   end;
  33.  
« Last Edit: June 26, 2017, 06:24:28 pm by Xander »

jmpessoa

  • Hero Member
  • *****
  • Posts: 2297
Re: Write to txt file on android
« Reply #1 on: June 25, 2017, 07:34:17 pm »
Hi Xander!

drop "IOUtils" from you project and try:

Code: Pascal  [Select][+][-]
  1. procedure TAndroidModule1.jButton1Click(Sender: TObject);
  2. var
  3.   txtFile : TextFile;
  4.   sFilePath: string;
  5.   sString: string;
  6. begin
  7.   sFilePath:=  self.GetEnvironmentDirectoryPath(dirDownloads) + '/' + 'MyTest.txt';
  8.   AssignFile(txtFile, sFilePath);
  9.  
  10.   sString:= 'Hello World!';
  11.  
  12.   if FileExists(sFilePath) then
  13.   begin
  14.       ShowMessage('True');
  15.       Append(txtFile);
  16.       WriteLn(txtFile,sString);
  17.       CloseFile(txtFile);
  18.   end
  19.   else
  20.   begin
  21.       ShowMessage('False');
  22.       Rewrite(txtFile);
  23.       WriteLn(txtFile,sString);
  24.       CloseFile(txtFile);
  25.   end;
  26. end;
  27.  

PS. Please, put LAMW in your subject...
« Last Edit: June 25, 2017, 07:36:36 pm by jmpessoa »
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Write to txt file on android
« Reply #2 on: June 25, 2017, 10:07:58 pm »
I think the OP needs TPath.GetHomePath which is documented as:
Quote
On iOS and Android, it points to the device-specific location of the sandbox for the application.
which translates to /data/data/<package.application>, this path is accessible only by root or the application itself.

jmpessoa

  • Hero Member
  • *****
  • Posts: 2297
Re: Write to txt file on android
« Reply #3 on: June 25, 2017, 10:43:45 pm »
I dont found unit "IOUtils" in "fpc 3.1.1" ...

but  LAMW  can handle intern app storage path:

Code: Pascal  [Select][+][-]
  1. sFilePath:=  self.GetEnvironmentDirectoryPath(dirInternalAppStorage) + '/' + 'MyTest.txt';
  2.  
« Last Edit: June 25, 2017, 11:24:18 pm by jmpessoa »
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

Xander

  • New Member
  • *
  • Posts: 14
Re: Write to txt file on android LAMW
« Reply #4 on: June 26, 2017, 06:27:30 pm »
Thanks alot

Xander

  • New Member
  • *
  • Posts: 14
Re: Write to txt file on android LAMW
« Reply #5 on: June 26, 2017, 07:07:48 pm »
I know this is not part of the topic but do any of u maybe know when
memos will be available in LAMW?

jmpessoa

  • Hero Member
  • *****
  • Posts: 2297
Re: Write to txt file on android LAMW
« Reply #6 on: June 26, 2017, 10:21:46 pm »
Ok. I will try implement the "jMemo" component!

But you can get a memo [like] just now:

try  [from AppDemo1  "unit14"]:
Quote
  object jEditText1: jEditText
    LayoutParamWidth = lpMatchParent
    LayoutParamHeight = lpOneQuarterOfParent
    InputTypeEx = itxMultiLine
    MaxTextLength = 3000
    MaxLines = 50                                 <<--- max visible lines!
    HorScrollBar = False
    VerScrollBar = True
    WrappingLine = True
  end
« Last Edit: June 26, 2017, 10:24:25 pm by jmpessoa »
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

Xander

  • New Member
  • *
  • Posts: 14
Re: Write to txt file on android LAMW
« Reply #7 on: June 27, 2017, 06:40:07 am »
 Thanks again for another quick reaponse. The edit works perfectly, now im facing another problem again :(. How can i load the text from the file into the edit? In delphi I used memo.Lines.LoadFromFile(sFilePath); obviously this does not work now because im not using a memo. LoadFromFile is not an obtion with JEditText.Text.
Code: Pascal  [Select][+][-]
  1. procedure TAndroidModule1.btnLoadClick(Sender: TObject);
  2. var
  3.   sLoaded : String;
  4. begin
  5.  
  6.   //Load from file
  7.   sFilePath := self.GetEnvironmentDirectoryPath(dirInternalAppStorage)+ '/' +
  8.   'MyTest.txt';
  9.   AssignFile(txtFile, sFilePath);
  10.   if FileExists(sFilePath) then
  11.   begin
  12.     Read(txtFile,sLoaded);
  13.     ShowMessage('Succes');
  14.     jEditText1.Text := sLoaded;
  15.     //mmo.Lines.LoadFromFile(sFilePath);
  16.     CloseFile(txtFile);
  17.   end else
  18.   begin
  19.     ShowMessage('Fail');
  20.   end;
  21. end;
  22.  
« Last Edit: June 27, 2017, 06:53:40 am by Xander »

jmpessoa

  • Hero Member
  • *****
  • Posts: 2297
Re: Write to txt file on android LAMW
« Reply #8 on: July 01, 2017, 01:36:04 am »
Quote
How can i load the text from the file into the edit?

Now you can!

Added LoadFromFile and SaveToFile to jEditText !

Please, update from github!

Thank you!
« Last Edit: July 01, 2017, 01:54:07 am by jmpessoa »
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

Xander

  • New Member
  • *
  • Posts: 14
Re: Write to txt file on android LAMW
« Reply #9 on: July 02, 2017, 11:29:57 am »
Im not sure if i did something wrong but I get this message even after updating LAMW. I downloaded with TortoiseSVN. I then ran Laz4Android and installed al the packages again.

Cyrax

  • Hero Member
  • *****
  • Posts: 836
Re: Write to txt file on android LAMW
« Reply #10 on: July 02, 2017, 11:38:07 am »
Im not sure if i did something wrong but I get this message even after updating LAMW. I downloaded with TortoiseSVN. I then ran Laz4Android and installed al the packages again.

Maybe this:
Code: Pascal  [Select][+][-]
  1. jEditText1.LoadFromFile('yourtextfile.txt');

The type of first parameter of LoadFromFile method is String, not TextFile.

Xander

  • New Member
  • *
  • Posts: 14
Re: Write to txt file on android LAMW
« Reply #11 on: July 02, 2017, 03:06:53 pm »
Hi im sorry that this is getting such a problem, I really dont know what im doing wrong again. I got an error again.

Cyrax

  • Hero Member
  • *****
  • Posts: 836
Re: Write to txt file on android LAMW
« Reply #12 on: July 02, 2017, 04:14:55 pm »
You probably need the code from jmpessoa's github page. And you need TortoiseGIT for it.

jmpessoa

  • Hero Member
  • *****
  • Posts: 2297
Re: Write to txt file on android LAMW
« Reply #13 on: July 03, 2017, 03:23:27 am »
Hello Xander!

Try:

"Run" ---> "Clean up and Build",  so you can update the [compiled] project files...


NOTE: You can use

"SaveToFlie/LoadFromFile(_filename: string)" for internal app storage...

and   

"SaveToFlie/LoadFromFile(_path: string; _filename: string)"  for publics  storage...
[dowmload, pictures, ..., etc ...]
« Last Edit: July 03, 2017, 03:58:11 am by jmpessoa »
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

Xander

  • New Member
  • *
  • Posts: 14
Re: Write to txt file on android LAMW
« Reply #14 on: July 15, 2017, 05:14:14 pm »
Today I completely deleted laz4android and LAMW from my laptop and reinstalled laz4android. I then Downloaded the LAMW zip file instead of updating with git repository or tortoise SVN. I then compiled and installed the three package files of LAMW into laz4android and set the paths. I then created i completely new GUI application with LAMW and inserted a jbutton and jeditText and STILL there is no LoadFromFile or SaveToFile method for jEditText.

 

TinyPortal © 2005-2018