2

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?

Ashwin
  • 7,277
  • 1
  • 48
  • 70
  • 2
    "Can I even pass the content URI in using ContentProvider?" -- yes, though `FileProvider` may not work for this use case. Since you already have a `ContentProvider`, and you already have the security set up for that provider, you might want to have your own `ContentProvider` serve the files. – CommonsWare Jul 31 '22 at 14:33

0 Answers0