0

I want the user to select a text file so the app can read its content. in onActivityResult I have the following code:

Uri filePath = data.getData();
File file = new File(filePath.getPath());

filePath.getPath() brings this weird path: /document/**1EEB-0A13:**Download/filename.txt

I don't understand what is 1EEB-0A13:, I'm almost certain that without it, it would work. Needless to say that I get an exception that there is no such file or directory.

I saw a lot of examples on how to get the real path but they seemed to be related to images. I tried one of them but it threw an exception.

Amos
  • 1,321
  • 2
  • 23
  • 44
  • i don't have explanation but please refer this: https://stackoverflow.com/questions/5568874/how-to-extract-the-file-name-from-uri-returned-from-intent-action-get-content – Jaydeep Devda May 30 '17 at 11:59
  • You should look at filePath.toString() and realise that it is not a file path but an uri with a content scheme. Use the content scheme instead of a file path. – greenapps May 30 '17 at 12:01
  • `1EEB-0A13` is the USB identifier for your SD card. Put in a different card and see that this identifier changes. – greenapps May 30 '17 at 12:01
  • `how to get the real path`. Dont try to do that anymore. Modern times now adays. Use the uri to open an InputStream is you want to read the content of the file. – greenapps May 30 '17 at 12:03
  • @greenapps thanks for the tip, please add it as a solution so I can accept it – Amos May 30 '17 at 12:17

2 Answers2

1

I want the user to select a text file so the app can read its content.

Unfortunately, you decided not to show the code for how you are doing this. I am going to assume that you are using ACTION_GET_CONTENT, or perhaps ACTION_OPEN_DOCUMENT. Neither of those have much to do with files.

filePath.getPath() brings this weird path: /document/**1EEB-0A13:**Download/filename.txt

getPath() only has meaning for a Uri with a file scheme. Yours has a content scheme, in which case the "path" is an opaque series of characters, having no meaning to you.

I'm almost certain that without it, it would work

I'm almost certain that it will not. For starters, not every Uri that you get back from ACTION_GET_CONTENT or ACTION_OPEN_DOCUMENT will look like that. The user may have any number of apps that respond to ACTION_GET_CONTENT, and any number of DocumentsProviders that plug into the ACTION_OPEN_DOCUMENT UI.

I saw a lot of examples on how to get the real path but they seemed to be related to images.

And they are all bug-riddled pieces of junk.

Use a ContentResolver and openInputStream() to get an InputStream on the content identified by the Uri.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
1

You should look at filePath.toString() and realise that it is not a file path but an uri with a content scheme. Use the content scheme instead of a file path.

1EEB-0A13 is the USB identifier for your SD card. Put in a different card and see that this identifier changes.

how to get the real path. Dont try to do that anymore. Modern times now adays. Use the uri to open an InputStream is you want to read the content of the file

greenapps
  • 11,154
  • 2
  • 16
  • 19