How to persist permission in android API 19 (KitKat)? How to persist permission in android API 19 (KitKat)? sqlite sqlite

How to persist permission in android API 19 (KitKat)?


I believe I've solved it. The request intent:

Intent intent;if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){    intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);    intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);    intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);}else{    intent = new Intent(Intent.ACTION_GET_CONTENT);}intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);intent.setType("image/*");startActivityForResult(Intent.createChooser(intent, getResources().getString(R.string.form_pick_photos)), REQUEST_PICK_PHOTO);

and onActivityResult

...// kitkat fixed (broke) content access; to keep the URIs valid over restarts need to persist access permissionif(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {    final int takeFlags = data.getFlags() & Intent.FLAG_GRANT_READ_URI_PERMISSION;    ContentResolver resolver = getActivity().getContentResolver();    for (Uri uri : images) {        resolver.takePersistableUriPermission(uri, takeFlags);    }}...

I haven't tested this pre-kitkat, my phone is running 5.1, can anyone verify this on older phones?


The code listed below,

// kitkat fixed (broke) content access; to keep the URIs valid over restarts need to persist access permissionif(Utils.isKitkat()){    final int takeFlags = data.getFlags() & (Intent.FLAG_GRANT_READ_URI_PERMISSION);    ContentResolver resolver = getActivity().getContentResolver();    for (Uri uri : images)    {        resolver.takePersistableUriPermission(uri, takeFlags);    }}

worked fine for me, anyway I noticed that the max number of persistable uri granted to my app is limited to 128. If I select more than 128 uri, I get the error:

java.lang.SecurityException: Permission Denial: opening provider........

just when I try to process an image for which I wasn't able to persist the permission. Can you figure out any solution?