MIME type for reading CSV file in an Android app does not work

74 Views Asked by At

maybe I can get help here. I'm working on an Android app in Java and try to pick a .csv file to import some data:

    public void onButtonImportClick(View view) {
        Log.d(TAG, "onButtonImportClick");
        Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        intent.setType("text/*");
        intent.putExtra(Intent.EXTRA_MIME_TYPES, new String[] { "text/csv", "text/plain" });
        someActivityResultLauncher.launch(intent);
    }

The picker shows files but all are greyed out, besides the .txt files. Actually I expected to get .csv and .txt files. Obviously something is not correct with MIME type setting.
Any ideas or suggestions?

1

There are 1 best solutions below

3
Hiren Bharodiya On

Use opencsv for read CSV file.

Add below gradle into gradle file.

implementation 'com.opencsv:opencsv:4.6'

Implement below function for read csv file

JFileChooser chooser = new JFileChooser();
int returnValue = chooser.showOpenDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION) { 
    File file = chooser.getSelectedFile();
    CSVReader reader = new CSVReader(new FileReader(file));
    List myEntries = reader.readAll();
}