Content provider grant uri permission Content provider grant uri permission android android

Content provider grant uri permission


What grant uri is using for?

The "grant Uri permissions" feature allows you to have a ContentProvider that is normally inaccessible by third parties, yet selectively allow access to individual Uri values to individual third-party apps for a short period of time (e.g., long enough to view the PDF that the provider serves).

What is the differences between grant uri permission true and false

android:grantUriPermissions="true" indicates that your Java code can use FLAG_GRANT_READ_URI_PERMISSION and FLAG_GRANT_WRITE_URI_PERMISSION for any Uri served by that ContentProvider.

android:grantUriPermissions="false" indicates that only the Uri values specified by child <grant-uri-permission> elements can be used with FLAG_GRANT_READ_URI_PERMISSION and FLAG_GRANT_WRITE_URI_PERMISSION.


Say you need to mail some file that is on your application cache directory.

No other apps can access that file, unless you specify that other apps can access content of your application. For this you create content provider, and say, all uri's in form content://com.your.app/file you 'redirect' to your application cache directory.

Some code:

File f = ...; // Some local file.Uri uri = Uri.parse("content://com.your.app/" + f.getName());Intent intent = new Intent(Intent.ACTION_SEND);intent.setType("text/plain");intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");intent.putExtra(Intent.EXTRA_TEXT, "Body");intent.putExtra(Intent.EXTRA_STREAM, uri);// You only can add flag FLAG_GRANT_READ_URI_PERMISSION if your app has // android:grantUriPermissions="true" in manifest or see quote below.intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);startActivity(Intent.createChooser(intent, "Send Email"));

As CommonsWare said:

android:grantUriPermissions="false" indicates that only the Uri values specified by child <grant-uri-permission> elements can be used with FLAG_GRANT_READ_URI_PERMISSION and FLAG_GRANT_WRITE_URI_PERMISSION.


       with adapter class and In Kotlin :-         val uri: Uri? = FileProvider.getUriForFile(mContext, BuildConfig.APPLICATION_ID, file)                val sharingIntent = Intent(Intent.ACTION_SEND)        sharingIntent.type = "image/*"        sharingIntent.putExtra(Intent.EXTRA_SUBJECT, "Images ")        sharingIntent.putExtra(Intent.EXTRA_STREAM, uri)                val chooser = Intent.createChooser(sharingIntent, "Share File")        val resInfoList: List<ResolveInfo> = mContext.packageManager            .queryIntentActivities(chooser, PackageManager.MATCH_DEFAULT_ONLY)        for (resolveInfo in resInfoList) {            val packageName = resolveInfo.activityInfo.packageName            mContext.grantUriPermission(                packageName,                uri,                Intent.FLAG_GRANT_WRITE_URI_PERMISSION or Intent.FLAG_GRANT_READ_URI_PERMISSION            )        }                mContext.startActivity(chooser)