当前位置 : 主页 > 手机开发 > android >

Android Intent选择CSV进行导入

来源:互联网 收集:自由互联 发布时间:2021-06-11
我想将特定的CSV文件导入数据库.我正在使用库aFileChooser来选择文件,但不会导入CSV文件中的数据.我哪里错了?谢谢 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {
我想将特定的CSV文件导入数据库.我正在使用库aFileChooser来选择文件,但不会导入CSV文件中的数据.我哪里错了?谢谢

@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(例外e){
   Log.e( “TAG”,e.toString());

}

使用“text / csv”类型和CATEGORY_OPENABLE类别打开intent:

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

现在在你的onActivityResult中:

case ACTIVITY_CHOOSE_FILE1: {
       if (resultCode == RESULT_OK){
            proImportCSV(new File(data.getData().getPath());
        }
      }
  }

现在我们需要更改你的proImportCSV方法来使用我们传回的实际文件:

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

    }
}
网友评论