To obtain the actual path using a FileProvider URI, you can follow these steps:
1) Get the content resolver instance:
ContentResolver resolver = context.getContentResolver();
2) Resolve the FileProvider URI to a file:
Uri uri = FileProvider.getUriForFile(context, AUTHORITY, file); File f = new File(uri.getPath());
3) Retrieve the absolute path of the file:
String path = f.getAbsolutePath();
Alternatively, if you need to get the path from a file descriptor obtained from the FileProvider URI, you can use the ParcelFileDescriptor API:
1) Get the content resolver instance:
ContentResolver resolver = context.getContentResolver();
2) Open the file descriptor for the FileProvider URI:
ParcelFileDescriptor parcelFileDescriptor = resolver.openFileDescriptor(uri, "r");
3) Get the actual file descriptor:
FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
4) Create a file object from the file descriptor:
File f = new File(fileDescriptor);
5) Retrieve the absolute path of the file:
String path = f.getAbsolutePath();
Note that in both cases, the actual path is not always guaranteed to be accessible, as the FileProvider may be serving files from different locations or even from an encrypted storage.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2023-07-13 19:06:05 +0000
Seen: 10 times
Last updated: Jul 13 '23
When using Polars and Glob to read Parquet from S3, why does it only read the first file?
What is the process for adding a class path permanently to the Mac terminal?
Why isn't the CSS background image being displayed?
What are the steps to install Homebrew in the /opt/ directory?
How can I insert a file path into Tkinter through drag and drop function?
What is preventing me from adding new static files to my springboot web application?
Using cucumber-junit, what is the process for configuring the path to my Cucumber features?
How can I extract the directory name from a given path in bash?