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());
}