Skip to content Skip to sidebar Skip to footer

Android Intent Choose Csv For Import

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? than

Solution 1:

Open the intent using the type "text/csv" and a Category of CATEGORY_OPENABLE:

privatevoidselectCSVFile(){
    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:

privatevoidproImportCSV(File from){
  try {
    // Delete everything above here since we're reading from the File we already haveContentValuescv=newContentValues();
    // reading CSV and writing tableCSVReaderdataRead=newCSVReader(newFileReader(from)); // <--- This line is key, and why it was reading the wrong fileSQLiteDatabasedb= 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();
       SimpleDateFormatcurrFormater=newSimpleDateFormat("dd-MM-yyyy");
       SimpleDateFormatpostFormater=newSimpleDateFormat("yyyy-MM-dd");

       String eDDte;
        try {
            DatenDate= 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());

    }
}

Solution 2:

The previous answer is perfect, but you need to change two things.

Open the intent using the type "/", as file manager in android doesn't let pick csv files if we use "text/csv":

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

Now in your onActivityResult trim the file path, as file path contain garbage path before storage or /storage. So you need to delete those characters to make your csv reader find the path:

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

more on file path

Solution 3:

                intent = newIntent(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);
            

Post a Comment for "Android Intent Choose Csv For Import"