sdabet
sdabet

Reputation: 18670

IllegalArgumentException in grantUriPermission on API level 19

The following line of code

context.getApplicationContext().grantUriPermission(packageName, uri, Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);

raises this exception when running on devices with API level 19 (KitKat), but not on later versions:

java.lang.IllegalArgumentException: Requested flags 0x40, but only 0x3 are allowed
  at android.os.Parcel.readException(Parcel.java:1476)
  at android.os.Parcel.readException(Parcel.java:1426)
  at android.app.ActivityManagerProxy.grantUriPermission(ActivityManagerNative.java:3461)
  at android.app.ContextImpl.grantUriPermission(ContextImpl.java:1732)
  at android.content.ContextWrapper.grantUriPermission(ContextWrapper.java:577)

Why is that so?

Upvotes: 7

Views: 1467

Answers (2)

nansjlee
nansjlee

Reputation: 419

I think this is a bug in KitKat.

https://android.googlesource.com/platform/frameworks/base/+/kitkat-mr2.2-release/services/java/com/android/server/am/ActivityManagerService.java#6214

Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION is missing in check condition.

from lolipop version, it works correctly

Upvotes: 3

Oliver Hemsted
Oliver Hemsted

Reputation: 563

I believe this is caused by a change added in KitKat which should have fixed content access but they broke it.

You would need to run a check using Build.VERSION.SDK_INT < 19 (ie. pre-KitKat)

if(Build.VERSION.SDK_INT < 19) {
    context.getApplicationContext().grantUriPermission(packageName, uri, Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
} else {
    takePersistableUriPermission(packageName, uri);
} 

http://developer.android.com/reference/android/content/ContentResolver.html#takePersistableUriPermission

Upvotes: 5

Related Questions