Android Intent选择CSV进行导入 [英] Android Intent choose CSV for import

查看:321
本文介绍了Android Intent选择CSV进行导入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将特定的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();
文件file1 = com.ipaulpro.afilechooser.utils.FileUtils.getFile(uri);
proImportCSV(file1);





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


$ b private void proImportCSV(File from){

File root = Environment.getExternalStorageDirectory();
File exportDir = new File(root.getAbsolutePath());
文件csvFile = new File(exportDir,getString(R.string.app_name)+。csv);

尝试{
ContentValues cv = new ContentValues();
//读取CSV并写入表格
CSVReader dataRead = new CSVReader(new FileReader(csvFile));
String [] vv = null; ((vv = dataRead.readNext())!= null){
cv.clear();
SimpleDateFormat currFormater = new SimpleDateFormat(dd-MM-yyyy);
SimpleDateFormat postFormater = new SimpleDateFormat(yyyy-MM-dd);

String eDDte;
尝试{
日期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());

}

解决方案

使用类型text / csv和类别CATEGORY_OPENABLE打开意图。 b

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

$ / code>

现在在您的onActivityResult中:

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





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

pre $私有无效proImportCSV(文件){
尝试{
//删除上面的所有内容,因为我们从文件中读取已经有
ContentValues cv = new ContentValues();
//读取CSV并写入表格
CSVReader dataRead = new CSVReader(new FileReader(from)); //< ---这行是关键字,它为什么读错文件

SQLiteDatabase db = mHelper.getWritableDatabase(); / / LEt只是把这个放在这里,因为你可能会使用它不止一次
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;
尝试{
日期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();
$ b $ catch(Exception e){Log.e(TAG,e.toString());

}
}


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

}

解决方案

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

    }
}

这篇关于Android Intent选择CSV进行导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆