Android持久化uri权限不会因设备重启而持续存在

时间:2016-12-12 20:39:08

标签: android xamarin storage-access-framework

尽管打电话

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

在文档树URI上,虽然ContentResolver.PersistedUriPermissions属性在设备重启之前包含请求的URI,但URI不会在设备重新启动时保留。文档树是这样获得的(每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);

有什么我想念的吗?这是在AOSP Lollipop上,无论是模拟器还是硬件。

1 个答案:

答案 0 :(得分:0)

以下代码对我有用:

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);
}