I need to send image files (as rows using cursor) through ContentProvider to another app. I am storing the content URI of the image for sharing it with the app when queried.
override fun query(uri: Uri, projection: Array<out String>?, selection: String?, selectionArgs: Array<out String>?, sortOrder: String?): Cursor? {
val cursor = MatrixCursor(arrayOf("id", "title", "image"))
val movie = getMovie()
val rowBuilder = cursor.newRow()
rowBuilder.add("id", movie.id)
rowBuilder.add("title", movie.title)
val fileUri = FileProvider.getUriForFile(context!!, "com.my.content.provider.fileProvider", movie.imageFile)
rowBuilder.add("image", fileUri.toString())
return cursor
}
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.my.content.provider.fileProvider"
android:enabled="true"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
But when the other app queries the ContentProvider I get the result cursor and the row with data, but it gets SecurityException: Permission Denial: opening provider androidx.core.content.FileProvider that is not exported from UID 10437
when accessing the image content URI.
val cursor: Cursor? = contentResolver.query(
Uri.parse("content://com.my.content.provider/movies/1"),
null,
null,
null,
null
)
val image = cursor.getString(cursor.getColumnIndexOrThrow("image")) // Getting image URI successfully.
val inStream = contentResolver.openInputStream(Uri.parse(image)) // Throws Exception
val bitmap = BitmapFactory.decodeStream(inStream)
How can I grant read permission to this URI? I can only find examples of sharing content URI through Intent. Can I even pass the content URI using ContentProvider?