skip to content

On Android, how do you let another app read one file or row from your content provider without exporting it, and what does FLAG_GRANT_READ_URI_PERMISSION grant?

level: middleimportance: should knowfreq 45%

answer

  1. content:// authority and path
  2. keep the provider unexported
  3. grantUriPermissions on the provider
  4. a flag on the intent
  5. FileProvider instead of file://

basics

~10 s

Keep the provider unexported, enable android:grantUriPermissions, and send the item's content:// URI in an intent carrying FLAG_GRANT_READ_URI_PERMISSION. The receiving app gets temporary read access to that URI only, with no permission of its own.

solid answer

~40 s

A content provider serves data through `content://` URIs made of its authority and a path, and other apps reach it with `ContentResolver`. Exporting it opens the whole provider to every app, or to every holder of its permission. For one item, keep `android:exported="false"`, set `android:grantUriPermissions="true"` (or list `<grant-uri-permission>` paths), and put the item's URI in the intent with `FLAG_GRANT_READ_URI_PERMISSION` (and `FLAG_GRANT_WRITE_URI_PERMISSION` if the receiver must write). The receiving component then gets temporary access to just those URIs. For files, the AndroidX `FileProvider` builds such URIs; a `file://` URI sent to another app throws `FileUriExposedException` from Android 7.0 (API 24). Today the system grants implicitly for `ACTION_SEND`, `ACTION_SEND_MULTIPLE` and `ACTION_IMAGE_CAPTURE`, but Android 17 adds a StrictMode check, `detectImplicitUriPermissionGrant()`, because Android 18 stops doing so: set the flag yourself.

code

kotlin · 10 lines
kotlin
fun Activity.shareReceipt(tripId: String) {
    val receipt = File(filesDir, "receipts/trip-$tripId.pdf")
    val uri = FileProvider.getUriForFile(this, "$packageName.fileprovider", receipt)
    val send = Intent(Intent.ACTION_SEND).apply {
        type = "application/pdf"
        putExtra(Intent.EXTRA_STREAM, uri)
        addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) // explicit grant, as Android 18 will require
    }
    startActivity(Intent.createChooser(send, null))
}

go deeper

for a junior

Recall what a content URI is made of, that apps reach providers through ContentResolver, and that file:// URIs must not leave the app.

for a middle

Explain the URI grant path step by step: unexported provider, grantUriPermissions, the grant flags on the intent, and FileProvider for files.

for a senior

Show you would find implicit grants with Android 17's StrictMode check before Android 18 removes them, and harden providers with path permissions and parameterized queries.

for a principal

Make per-URI grants the team's default for sharing, so exporting a provider becomes a reviewed exception with a permission behind it.

## Content providers and content URIs A **content provider** (`ContentProvider`) is the Android component that offers structured data or files to other processes through a uniform interface: `query()`, `insert()`, `update()`, `delete()`, `getType()` and, for files, `openFile()`. Clients never call the provider object directly; they call a **`ContentResolver`**, and the system routes the call to the provider, starting its process if needed. Every item is addressed by a **content URI**: - `content://` is the scheme; - the **authority** names the provider and is declared in `android:authorities`, usually derived from the package name, such as `com.example.ride.provider`; - the **path** names a table or file, and an optional trailing ID names one row, as in `content://com.example.ride.provider/trips/42`. A provider's methods except `onCreate()` can be called by several threads at once, so they must be thread-safe, and `onCreate()` should stay light because it runs on the main thread during startup. ## Who can reach a provider | Control | Scope | |---|---| | `android:exported` | whether other apps can reach the provider at all | | `android:permission` | one read-write permission for the whole provider | | `android:readPermission`, `android:writePermission` | separate provider-wide permissions; they take precedence over `android:permission` | | `<path-permission>` | permissions for specific content URIs; they take precedence over provider-wide ones | | URI grants | temporary access to individual URIs carried by an intent | Set `android:exported` explicitly: on API level 16 and lower a provider defaulted to exported, and once exported with no permission any app can read and write it. Sharing a whole provider only between your own apps is done with a permission at signature level (declaring that permission is part of the manifest and permissions lesson). ## Temporary URI grants A grant gives one app access to specific URIs **without** it holding any permission, which suits "show this receipt" or "attach this photo": 1. On the `<provider>`, set `android:grantUriPermissions="true"`, or keep it `false` and add `<grant-uri-permission>` children for the paths that may be granted. When the attribute is absent it counts as `false`. 2. Put the URI in the intent's data, or in `EXTRA_STREAM` or the clip data for sharing. 3. Add `Intent.FLAG_GRANT_READ_URI_PERMISSION`, `FLAG_GRANT_WRITE_URI_PERMISSION` or both. 4. Start the other app's component with that intent. The system grants access to those URIs for the receiving component, temporarily; call `revokeUriPermission()` when a URI you granted stops being valid. ## Sharing files with FileProvider Files need the same treatment. Sending a `file://` URI outside the app throws **`FileUriExposedException`** for apps targeting Android 7.0 (API level 24) or higher, and it would expose a path the receiver may not be able to read. The AndroidX core library's **`FileProvider`** is a ready-made provider: declare it unexported with `android:grantUriPermissions="true"` and an XML resource listing shareable directories, then call `FileProvider.getUriForFile(context, authority, file)` to turn a private file into a `content://` URI you can grant. A typical declaration, with the paths resource limiting which directories can be shared: ```xml <provider android:name="androidx.core.content.FileProvider" android:authorities="${applicationId}.fileprovider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" /> </provider> ``` On the **receiving side**, the other app opens the stream with `contentResolver.openInputStream(uri)` and asks for the type with `getType()`; it must not expect a file path, because a content URI may be backed by a database row, a file or a stream the provider generates. ## What Android 17 changes Today, when an app launches an intent with a URI and the action `ACTION_SEND`, `ACTION_SEND_MULTIPLE` or `ACTION_IMAGE_CAPTURE`, the system **automatically** grants the URI permissions to the target. Android 17's behaviour changes announce that **starting in Android 18** the system will no longer do this, so Android 17 adds: - a StrictMode VM policy check, `detectImplicitUriPermissionGrant()`, that reports intents relying on the implicit grant; - a log message, "Please set the grant explicitly in the app", that you can find with `adb logcat`. The fix is to add `FLAG_GRANT_READ_URI_PERMISSION` to `ACTION_SEND` and `ACTION_SEND_MULTIPLE` intents, and both read and write flags to `ACTION_IMAGE_CAPTURE` intents. ## Querying safely When your provider builds SQL from a caller's input, use the `selection` with `selectionArgs` placeholders rather than concatenating values, or an exported provider becomes an SQL injection point. In the ride-hailing app, the trip receipt PDF lives in private storage, is shared through `FileProvider` with an explicit read grant, and the trips provider stays unexported.

  • What is the difference between android:grantUriPermissions="true" and listing <grant-uri-permission> elements?
    `true` lets intents grant temporary access to any URI of the provider, overriding its provider-level and path-level permissions for that grant. With `false` (the default), only the URIs or path patterns listed in `<grant-uri-permission>` children can be granted, which keeps the grantable part of the provider small.
  • How do you find, before Android 18, the places in your app that rely on implicit URI grants?
    On Android 17, enable a StrictMode VM policy with `detectImplicitUriPermissionGrant()` and `penaltyLog()`, or watch Logcat for 'Please set the grant explicitly in the app'. Each hit is an `ACTION_SEND`, `ACTION_SEND_MULTIPLE` or `ACTION_IMAGE_CAPTURE` intent that needs explicit grant flags.

  • Exporting the provider is the only way to share one row with another app
  • A file:// URI works fine for sharing a private file with another app
  • FLAG_GRANT_READ_URI_PERMISSION gives the receiver permanent access to the whole provider
  • android:grantUriPermissions defaults to true, so no manifest change is needed
  • Share intents will always get their URI grant from the system automatically

Assumes Android 17 (API 37), which adds detectImplicitUriPermissionGrant() ahead of Android 18 ending implicit URI grants for ACTION_SEND, ACTION_SEND_MULTIPLE and ACTION_IMAGE_CAPTURE; FileUriExposedException applies to apps targeting Android 7.0 (API 24).