Android - Get real path of a .txt file selected from the file explorer
I didn't find how I can get the file from the Uri.
There is no file. ACTION_OPEN_DOCUMENT
and ACTION_GET_CONTENT
do not open a file. They open a document. That document might be a file. It might not.
That Uri
might point to:
- A local file on external storage
- A local file on internal storage for the other app
- A local file on removable storage
- A local file that is encrypted and needs to be decrypted on the fly
- A stream of bytes held in a
BLOB
column in a database - A piece of content that needs to be downloaded by the other app first
- ...and so on
How can I get the real path of the file ?
You don't.
If you wish to only accept files, integrate a file chooser library instead of using ACTION_OPEN_DOCUMENT
orACTION_GET_CONTENT
. Just bear in mind that filesystem access to external storage is limited on Android 10+.
If you use ACTION_GET_CONTENT
, and the scheme of the Uri
that you get is file
, then getPath()
will be a filesystem path.
Otherwise, you need to understand that you have no idea where the document is coming from, and stop thinking in terms of "real path of the file". Use ContentResolver
and openInputStream()
to make a copy of the content to some file that you control, then work with that file.
How to get selected folder path from file picker in Android
You're not supposed to try to get a File
's path from a Uri
. You just use the Uri
itself as your reference to the file's location.
Besides, the user selected the location themselves, so they already know where it is.
Related Topics
How to Get the Download Url from Firebase Storage
Should I Actually Remove the Valueeventlistener
"No X11 Display Variable" - What Does It Mean
What's the Simplest Way to Print a Java Array
How to Declare and Initialize an Array in Java
Validating Input Using Java.Util.Scanner
How to Read Xml Using Xpath in Java
How to Get the Current Working Directory in Java
How to "Scan" a Website (Or Page) For Info, and Bring It into My Program
Can't Start Eclipse - Java Was Started But Returned Exit Code=13
Firebase Android Listview Not Being Displayed
How to Execute Bash Command With Sudo Privileges in Java
How to Print My Java Object Without Getting "Sometype@2F92E0F4"
What Is the Reason Behind "Non-Static Method Cannot Be Referenced from a Static Context"
How to Retrieve and Display Images from a Database in a Jsp Page
What's the Difference Between Next() and Nextline() Methods from Scanner Class