1

Despite calling

ContentResolver.TakePersistableUriPermission(uri, ActivityFlags.GrantReadUriPermission | ActivityFlags.GrantWriteUriPermission)

on a document tree URI, the URI does not persist over device reboots, although the ContentResolver.PersistedUriPermissions property contains the requested URI before the device reboot. The document tree is obtained like this (per Storage Access Framework persist permissions not working):

var intent = new Intent(Intent.ActionOpenDocumentTree);
intent.AddFlags(
    ActivityFlags.GrantReadUriPermission |
    ActivityFlags.GrantWriteUriPermission |
    ActivityFlags.GrantPersistableUriPermission | 
    ActivityFlags.GrantPrefixUriPermission);
StartActivityForResult(intent, INTENT_SELECT_TREE);

Is there anything I'm missing? This is on AOSP Lollipop, both in emulator and on hardware.

Community
  • 1
  • 1
LucasMcGraw
  • 129
  • 3
  • 12
  • You threw away the flags you got. In the link you provided you see `int takeFlags = data.getFlags() & ( Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);`. You are not using that. – greenapps Dec 12 '16 at 21:10
  • Further it does not make sense trying to add flags on an intent used by startactivityforresult. Only the provider can grant the permissions. Not you the consumer. – greenapps Dec 12 '16 at 21:11

1 Answers1

0

The following code is working for me:

protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
{
    base.OnActivityResult(requestCode, resultCode, data);

    var androidUri = data.Data;
    var takeFlags = data.Flags & (ActivityFlags.GrantReadUriPermission | ActivityFlags.GrantWriteUriPermission);
    ContentResolver.TakePersistableUriPermission(androidUri, takeFlags);
}
Mohammad Nikravan
  • 1,543
  • 2
  • 19
  • 22