Recent

Author Topic: Create CSV  (Read 8360 times)

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Create CSV
« on: December 06, 2021, 03:11:13 pm »
Hello to all. Can anyone tell me how I can export a csv file to the Download folder? Thanks
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

loaded

  • Hero Member
  • *****
  • Posts: 824
Re: Create CSV
« Reply #1 on: December 06, 2021, 04:38:19 pm »
Hi ,
lazandroidmodulewizard-master\demos\GUI\AppDownloadManagerDemo1 I think you can do whatever you want with the sample project.
Check out  loaded on Strava
https://www.strava.com/athletes/109391137

jmpessoa

  • Hero Member
  • *****
  • Posts: 2297
Re: Create CSV
« Reply #2 on: December 06, 2021, 09:04:37 pm »
Quote
how I can export a csv file to the Download folder...

That is,  save some csv/txt content to a  file in device Download folder?
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: Create CSV
« Reply #3 on: December 07, 2021, 08:39:13 am »
Yes. I test this code. But not create a file.

filename   := self.GetEnvironmentDirectoryPath(dirDownloads) + System.DirectorySeparator + 'abcd.csv';
Self.jListView1.SaveToFile(filename);
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

jmpessoa

  • Hero Member
  • *****
  • Posts: 2297
Re: Create CSV
« Reply #4 on: December 07, 2021, 06:21:08 pm »

Maybe, our app can not save to "public" folder anymore....

The system is imposing new restrictions with each new Android version..

Can you test in some "old" device?

What about jTextFileManager? can you do some test?

Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Create CSV
« Reply #5 on: December 09, 2021, 05:53:22 am »
You need WRITE_EXTERNAL_STORAGE in the manifest these days. Well, depending on the target API.

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: Create CSV
« Reply #6 on: February 10, 2022, 05:00:52 pm »
With this code my app crash. Why?
Code: Pascal  [Select][+][-]
  1. procedure TAndroidModuleMain.Btn_EsportaClick(Sender: TObject);
  2. var
  3.    nome_file   : string;
  4. begin
  5.  
  6.      if not IsRuntimePermissionGranted('android.permission.WRITE_EXTERNAL_STORAGE') then
  7.      begin
  8.           ShowMessage('Sorry... Some Runtime Permission NOT Granted ...');
  9.           Exit;
  10.      end;
  11.  
  12.      nome_file   := self.GetEnvironmentDirectoryPath(dirDownloads) + System.DirectorySeparator + 'letture_barcode.csv';
  13.      if FileExists(nome_file) then
  14.      begin
  15.           DeleteFile(nome_file);
  16.      end;
  17.      Self.jTextFileManager1.SaveToFile('hello world', nome_file);
  18.      ShowLog('Esportato in: ' + nome_file);
  19. end;    
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

jmpessoa

  • Hero Member
  • *****
  • Posts: 2297
Re: Create CSV
« Reply #7 on: February 10, 2022, 07:52:22 pm »
Hi, xinyiman!

Android 11 - Api 30  has restricted developers to access devices "publics" folders (ex: "Download, Documents, ..., etc")

Developers no longer have access to a file via its path. Instead, you need to use via “Uri“.

New!   jForm methods:

 //get user permission... and an uri
RequestCreateFile
RequestOpenFile
RequestOpenDirectory
procedure TakePersistableUriPermission

 //handling file by uri....
 function GetFileList                [edited...]
 function GetBitmapFromUri
 function GetTextFromUri
 procedure SaveImageToUri
 procedure SaveTextToUri

New demo:
"AppPublicFoldersAccessDemo1"
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: Create CSV
« Reply #8 on: February 10, 2022, 08:49:40 pm »
Ok it works. Just one problem. It appears that saving the data with SaveTextToUri is asynchronous. It took several seconds to write 12 lines. Do you know why?
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: Create CSV
« Reply #9 on: February 10, 2022, 09:02:32 pm »
Correct me, it's not asynchronous. It does not save the data in the file unless I change the screen. Do you know how to make it immediate?
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

Insid3Code

  • New Member
  • *
  • Posts: 26
  • Code Immersion
Re: Create CSV
« Reply #10 on: February 10, 2022, 10:09:08 pm »
The crash is due to: path separator "/"
Code: Pascal  [Select][+][-]
  1. java.lang.IllegalArgumentException: File /storage/emulated/0/Download/letture_barcode.csv contains a path separator
  2.  

Code: Pascal  [Select][+][-]
  1. public void SaveToFile(String _txtContent, String _filename) {        
  2.      try {
  3.          OutputStreamWriter outputStreamWriter = new OutputStreamWriter(context.openFileOutput(_filename, Context.MODE_PRIVATE));
  4. ..
  5. ..
  6. ..
  7. ...
  8.      }
  9.      catch (IOException e) {
  10.  ...
  11.      }
  12. }
  13.  
openFileOutput don't accept "/" path separator.

You can fix your snippet like this:
jTextFileManager1.SaveToFile('hello world', Path, nome_file);

Code: Pascal  [Select][+][-]
  1. procedure TAndroidModule1.Button1Click(Sender: TObject);
  2. var
  3.   nome_file, Path: string;
  4. begin
  5.   nome_file := '/letture_barcode.csv';
  6.   if not IsRuntimePermissionGranted('android.permission.WRITE_EXTERNAL_STORAGE') then
  7.   begin
  8.     ShowMessage('Sorry... Some Runtime Permission NOT Granted ...');
  9.     Exit;
  10.   end;
  11.  
  12.   Path := self.GetEnvironmentDirectoryPath(dirDownloads);
  13.  
  14.   if FileExists(Path + nome_file) then
  15.   begin
  16.     DeleteFile(nome_file);
  17.   end;
  18.  
  19.   jTextFileManager1.SaveToFile('hello world', Path, nome_file);
  20.  
  21.   if FileExists(Path + nome_file) then
  22.   begin
  23.     ShowMessage('Done!');
  24.   end;
  25. end;                              
Animated gif as demo
Code: Pascal  [Select][+][-]
  1. public void SaveToFile(String _txtContent, String _path, String _filename){
  2.         FileWriter fWriter;    
  3.         try{ // Environment.getExternalStorageDirectory().getPath()
  4.              fWriter = new FileWriter(_path +"/"+ _filename);
  5.              fWriter.write(_txtContent);
  6.              fWriter.flush();
  7.              fWriter.close();
  8.          }catch(Exception e){
  9.              e.printStackTrace();
  10.          }
  11. }  
  12.  

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: Create CSV
« Reply #11 on: February 11, 2022, 08:43:27 am »
You're right, it works. But now I'm curious to know why the solution proposed by jmpessoa creates the empty file and fills it only after I interact with other forms of the project.
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

 

TinyPortal © 2005-2018