0

In my app I am importing a csv file. This works fine when using the internal memory but it does not work from the SD card.

The following code is used to show the file dialog:

Intent intent = new Intent().setType("*/*").setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select a file"), 99);

Afterwards the selected file gets used like this:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 99 && resultCode == RESULT_OK) {
            Uri selectedFile = data.getData();
            this.readFromCSV(selectedFile.getPath());
        }
    }

When I pick a file from the internal storage (from the folder "Documens" the returned path looks like this and works:

/storage/emulated/0/Documents/test.csv

From the SD card (from the root level) like this and fails:

/storage/emulated/0/document/78F1-1915:test.csv

Unfortunately DocumentFile.fromSingleUri() also returns the same invalid path.

How do I fix the path for the SD card file? Cheers!

Luigi04
  • 437
  • 5
  • 14

0 Answers0