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());
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…