16

I searched and read android developer files but I did not find any satisfactory answer for content provider grant uri permission. Can anybody explain more detailed and more simple. My questions are: What grant uri is using for? What is the differences between grant uri permission true and false When we should use true? when false? and any more detail are appreciated.

Android
  • 161
  • 1
  • 1
  • 3

3 Answers3

13

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 2
    I am still at the same point and dont understand why grant uri is using, when we should use. Can you explain more detailed and tell me like a junior developer. – Android Apr 21 '14 at 08:24
  • @Android What @CommonsWare meant is if `android:grantUriPermissions` is set to false the Android system will check if manifest has `` tag with specific pathnames that should be allowed to use URI permissions like ` ` – KingKongCoder Jan 06 '19 at 20:57
8

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.

Borzh
  • 5,069
  • 2
  • 48
  • 64
0
       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)
Krishna
  • 1,556
  • 13
  • 21