Ask Your Question
2

What is the process for obtaining the actual path using a FileProvider URI?

asked 2023-07-13 19:06:05 +0000

huitzilopochtli gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2023-07-13 19:12:02 +0000

qstack gravatar image

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.

edit flag offensive delete link more

Your Answer

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

Add Answer


Question Tools

Stats

Asked: 2023-07-13 19:06:05 +0000

Seen: 10 times

Last updated: Jul 13 '23