Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export my data on CSV file from app android

I have a class:

public class A() {
private List<B> e;
private int nE;
private int nC;
private int tC;
private int bL;
private float mA;
private float mP;
private int eP;`      }

And a second class:

public class B() {
private String m;
private int v;
private int l;
private int c;
private String d;
private String n;
private Calendar data;
private int t;   }

How can i do to save this information into a CSV file from my app? I would offer compatibility from 2.3.3 to 4.2.2, so i wouldn't use library that cannot do this. Can you help me wirte some code? Thank you all!!

Another thing..to export this file what permission i must add in manifest.xml?

like image 755
user2520969 Avatar asked Jul 15 '13 00:07

user2520969


1 Answers

You'll need to use a library such as opencsv (found here: http://sourceforge.net/projects/opencsv/)

To write data to a file you'll need to do something similar to this:

String csv = android.os.Environment.getExternalStorageDirectory().getAbsolutePath();
CSVWriter writer = new CSVWriter(new FileWriter(csv));

List<String[]> data = new ArrayList<String[]>();
data.add(new String[] {"India", "New Delhi"});
data.add(new String[] {"United States", "Washington D.C"});
data.add(new String[] {"Germany", "Berlin"});

writer.writeAll(data);

writer.close();

(modified from here: http://viralpatel.net/blogs/java-read-write-csv-file/)

to write a file to storage you will need the following permission:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
like image 131
William Pownall Avatar answered Sep 17 '22 15:09

William Pownall