Android Intent choose CSV for import
android, android-intent, csv, file
Solution
Open the intent using the type "text/csv" and a Category of CATEGORY_OPENABLE:
private void selectCSVFile(){
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("text/csv");
startActivityForResult(Intent.createChooser(intent, "Open CSV"), ACTIVITY_CHOOSE_FILE1);
}
Now in your onActivityResult:
case ACTIVITY_CHOOSE_FILE1: {
if (resultCode == RESULT_OK){
proImportCSV(new File(data.getData().getPath());
}
}
}
And now we need to change your proImportCSV method to use the actual File we're passing back:
private void proImportCSV(File from){
try {
// Delete everything above here since we're reading from the File we already have
ContentValues cv = new ContentValues();
// reading CSV and writing table
CSVReader dataRead = new CSVReader(new FileReader(from)); // <--- This line is key, and why it was reading the wrong file
SQLiteDatabase db = mHelper.getWritableDatabase(); // LEt's just put this here since you'll probably be using it a lot more than once
String[] vv = null;
while((vv = dataRead.readNext())!=null) {
cv.clear();
SimpleDateFormat currFormater = new SimpleDateFormat("dd-MM-yyyy");
SimpleDateFormat postFormater = new SimpleDateFormat("yyyy-MM-dd");
String eDDte;
try {
Date nDate = currFormater.parse(vv[0]);
eDDte = postFormater.format(nDate);
cv.put(Table.DATA,eDDte);
}
catch (Exception e) {
}
cv.put(Table.C,vv[1]);
cv.put(Table.E,vv[2]);
cv.put(Table.U,vv[3]);
cv.put(Table.C,vv[4]);
db.insert(Table.TABLE_NAME,null,cv);
} dataRead.close();
} catch (Exception e) { Log.e("TAG",e.toString());
}
}
Problem
I want to import a specific CSV file into the database. I'm using the library aFileChooser to choose the file, but the data in the CSV file are not imported. Where am I wrong? thanks ``` @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch(requestCode) { case ACTIVITY_CHOOSE_FILE1: { if (resultCode == RESULT_OK){ Uri uri = data.getData(); File file1 = com.ipaulpro.afilechooser.utils.FileUtils.getFile(uri); proImportCSV(file1); } } } } ricevi_csv= (Button) findViewById(R.id.but_ricevi_csv); ricevi_csv.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent chooseFile; Intent intent; chooseFile = new Intent(Intent.ACTION_GET_CONTENT); chooseFile.setType("application/image"); intent = Intent.createChooser(chooseFile, "Choose a CSV"); startActivityForResult(intent, ACTIVITY_CHOOSE_FILE1); } }); } private void proImportCSV(File from){ File root = Environment.getExternalStorageDirectory(); File exportDir = new File(root.getAbsolutePath()); File csvFile = new File(exportDir, getString(R.string.app_name)+".csv"); try { ContentValues cv = new ContentValues(); // reading CSV and writing table CSVReader dataRead = new CSVReader(new FileReader(csvFile)); String[] vv = null; while((vv = dataRead.readNext())!=null) { cv.clear(); SimpleDateFormat currFormater = new SimpleDateFormat("dd-MM-yyyy"); SimpleDateFormat postFormater = new SimpleDateFormat("yyyy-MM-dd"); String eDDte; try { Date nDate = currFormater.parse(vv[0]); eDDte = postFormater.format(nDate); cv.put(Table.DATA,eDDte); } catch (Exception e) { } cv.put(Table.C,vv[1]); cv.put(Table.E,vv[2]); cv.put(Table.U,vv[3]); cv.put(Table.C,vv[4]); SQLiteDatabase db = mHelper.getWritableDatabase(); db.insert(Table.TABLE_NAME,null,cv); ``` } dataRead.close(); } catch (Exception e) { Log.e("TAG",e.toString()); }