FileProvider
Also known as: android fileprovider, content uri sharing, FileProvider.getUriForFile
FileProvider is an Android component that grants other apps temporary, permission-scoped access to your files by handing out a content:// URI instead of a raw file path. It is the standard way to share or open a file safely on modern Android.
- Replaces raw file:// URIs, which throw FileUriExposedException on Android 7.0+ when shared across apps.
- Call FileProvider.getUriForFile() to turn a File into a content:// URI declared via manifest XML paths.
- Pair the URI with FLAG_GRANT_READ_URI_PERMISSION to grant the receiving app temporary, revocable access.
What FileProvider does
FileProvider is a subclass of ContentProvider in AndroidX that maps a file on your app's private storage to a `content://` URI. Since Android 7.0 (API 24), passing a raw `file://` URI across app boundaries throws a FileUriExposedException, so sharing or opening a file with another app must go through a content URI. FileProvider is the official mechanism for producing one.
You declare it in the manifest with an `<provider>` entry pointing at an XML `meta-data` file that lists which directories it may expose (for example `cache-path`, `files-path`, or `external-files-path`). At runtime you call FileProvider.getUriForFile() to convert a `File` into a shareable URI.
Granting temporary access
A content URI on its own is not enough — the receiving app also needs permission to read it. You attach FLAG_GRANT_READ_URI_PERMISSION (and optionally write) to the Intent that carries the URI, which grants the target app access only for the life of that Intent rather than a permanent permission.
This is what makes FileProvider safe: instead of exposing your entire storage path, you hand out a single, revocable handle to one file. It is used everywhere a file leaves the app — `ACTION_SEND` share sheets, `ACTION_VIEW` to open a document, and supplying a camera app an output URI for a captured photo.
Why a cleaner relies on it
A storage cleaner constantly needs to let users act on the files it finds — share a large video, open a duplicate in a viewer to confirm it, or hand a file to another app. Under scoped storage, the cleaner cannot pass a bare file path, so each of those actions routes through FileProvider.getUriForFile() plus a read grant.
This keeps the cleanup flow within Android's security model: the app exposes exactly the one file the user chose to act on, for exactly as long as the action runs, and nothing more.