IllegalArgumentException in grantUriPermission by API Level 19

Next line of code

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

throws this exception when working on devices with API level 19 (KitKat), but not in 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 this so?

+5
source share
2 answers

I believe this is caused by a change added to KitKat that should have fixed access to the content, but they broke it.

You will need to run the check using Build.VERSION.SDK_INT <19 (i.e. 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

+2
source

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 not in validation state.

from lolipop version, it works correctly

0
source

Source: https://habr.com/ru/post/1240423/


All Articles