Skip to content

Content Provider

The Content Provider functions as an intermediary, facilitating access to the private data or files of your application by other applications.

Content Provider

FileProvider (Share files with other apps)

Section titled “FileProvider (Share files with other apps)”
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/paths" />
</provider>

Give other apps permission to access app private external storage

Section titled “Give other apps permission to access app private external storage”

Create res/xml/paths.xml:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-files-path name="external_dir" path="." />
</paths>
val cameraCaptureUri = FileProvider.getUriForFile(
context,
"${context.packageName}.provider",
File(context.getExternalFilesDir("image"), "image_${System.currentTimeMillis()}.png")
)

⚠️ Make sure the requested path is defined in the FILE_PROVIDER_PATHS resource, or else you’ll get a security exception.