0

I am trying to get the file path of a file from a file picker.

My problem is, that when I output the file path in text form, I get the following output:

file:/storage/emulated/~rest_of_path~

I assume, to access the file, the path should begin something like this:

file:///storage/emulated/~rest_of_path~

or at least file://

Why do I only get file:/ i.e a single slash.

The code in charge for getting the file path:

    ...
File fileToImport = new File(actualfilepath);

                    String tempPath =  uri.getPath();
                    if (tempPath.contains("//")){
                        tempPath = tempPath.substring(tempPath.indexOf("//")+1);
                    }
                    if ( actualfilepath.equals("") || actualfilepath.equals(" ")) {
                        fileToImport = new File(tempPath);
                    }else {
                        fileToImport = new File("file://"+actualfilepath);
                    }


                    // TODO: 2019-06-04
                    functionforfile(fileToImport)
                    filePath.setText(fileToImport.toString());

The text view for the code filePath.setText(fileToImport.toString());

shows me the file path for debug purpose only

if fileToImport = new File("file://"+actualfilepath); has File("file://"actualfilepath) instead, it outputs the file path without file:// which I understand, but why do the two // get removed when I add "file://" + actualfilepath

How could I add the correct file:///

Thanks in advance

Frankie Boyle
  • 29
  • 1
  • 4
  • "I am trying to get the file path of a file from a file picker" -- if by "file picker", you mean `ACTION_OPEN_DOCUMENT` or `ACTION_GET_CONTENT`, those are not "file" pickers, and you are not getting files. If by "file picker" you mean a file-picker library, bear in mind that Android Q and R are eliminating access to external storage, so you should consider switching to `ACTION_OPEN_DOCUMENT` and working with the `content:` `Uri` values it gives you. – CommonsWare Jun 11 '19 at 20:38
  • ok, I use ACTION_GET_CONTENT – Frankie Boyle Jun 11 '19 at 20:41
  • I suggest that you read https://stackoverflow.com/questions/49221312/android-get-real-path-of-a-txt-file-selected-from-the-file-explorer. If the scheme of your `Uri` is `file`, then you already have a `file` `Uri` and do not need to do anything further. If the scheme of your `Uri` is `content`, it does not necessarily point to a file on the filesystem that you can access. Just use the `content` `Uri`, via `ContentResolver` and `DocumentFile`. – CommonsWare Jun 11 '19 at 20:46

0 Answers0