【问题标题】:Android Intent choose CSV for importAndroid Intent 选择 CSV 进行导入
【发布时间】:2014-07-03 01:43:12
【问题描述】:

我想将特定的 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);

} 数据读取.close();

} 捕捉(异常 e){ Log.e("TAG",e.toString());

}

【问题讨论】:

  • 您是否遇到任何异常?我能看到的唯一奇怪的事情是你得到的是“应用程序/图像”,而不是使用“文本/csv”和 Intent.ACTION_GET_CONTENT 而不是 Intent.CATEGORY_OPENABLE。您甚至可能想使用“new File(data.getData().getPath())”而不是“com.ipaulpro.afilechooser.utils.FileUtils.getFile(uri);” -- 我昨天刚刚做了这个 CSV 重要的东西,这对我来说似乎工作正常。您的阅读内容看起来不错,尽管您可能希望在进入 while 循环之前调用 dataRead.readNext() 以确保跳过标题行(如果有的话)
  • 你能告诉我你的代码吗? Intent.CATEGORY_OPENABLE 不适合我
  • 当然,我会把它作为答案发布。请给我一点时间从 github 中提取它
  • 我错了删除 ACTION_GET_CONTENT btw

标签: android file csv android-intent


【解决方案1】:

使用“text/csv”类型和 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);
}

现在在你的 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());

    }
}

【讨论】:

  • intent.setType("text/csv");不适用于常规文件浏览器,我可以从中选择任何文件。但它确实对仅显示 .csv 文件的 Dropbox 应用程序有影响。如何为常规文件浏览器做同样的事情?
  • 试试intent.setType("text/comma-separated-values");
【解决方案2】:

前面的答案很完美,但是你需要改变两点。

使用“/”类型打开意图,因为如果我们使用“text/csv”,android 中的文件管理器不会让选择 csv 文件:

private void selectCSVFile(){
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    intent.setType("*/*");
    startActivityForResult(Intent.createChooser(intent, "Open CSV"),             ACTIVITY_CHOOSE_FILE1);
}

现在在您的 onActivityResult 中修剪文件路径,因为文件路径包含存储或 /storage 之前的垃圾路径。所以你需要删除这些字符才能让你的 csv 阅读器找到路径:

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

more on file path

【讨论】:

    【解决方案3】:
                    intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
                    intent.addCategory(Intent.CATEGORY_OPENABLE);
                    intent.setType("text/comma-separated-values");
                    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    startActivityForResult(intent, CREATE_FILE);
                
    

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 2015-08-12
    • 1970-01-01
    • 2013-05-06
    • 1970-01-01
    • 2022-10-23
    • 1970-01-01
    • 2014-05-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多