问题描述:

In my code, you can either import or export csv files. The export function works well, for the creation of the file, I use this for the path:

Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)

It returns the following path:

/storage/emulated/0/Download/File.CSV

In my Import function, I use a createchooser which gets a file explorer to find the file to import. If I select the same file and ask for the path with the following code:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

switch (requestCode) {

case REQUEST_FILE_CODE:

if (resultCode == RESULT_OK) {

Uri uriFile = data.getData();

Log.v("testimport", uriFile.getPath());

File fileToImport = new File(uriFile.getPath());

BufferedReader reader = new BufferedReader(new FileReader(fileToImport));

I get a file not found error with the following path:

/document/primary:Download/StudyMemory/File.csv

Why do I get two differents paths? How can I get the absolute path of the uri coming from the file explorer?

Thanks in advance.

网友答案:

The first 'path is a file system path. The second one a content provider path.

As you want to read the file you better open an input stream like

 InputStream is = getContentResolver().openInputSteam(uriFile);

And use the stream for your readers.

相关阅读:
Top