Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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.