Renaming Google Drive document using Android Storage Access Framework results in permission errors
<p>I am using android's storage access framework (SAF) to interact with documents.  I have been able to find/read/write documents without any trouble, but I have run into issues when I attempt renaming using DocumentsContract#renameDocument() with Google Drive documents.  The code I have posted works fine with files in Internal Storage.</p>

<p>To make things easy, I made a sample app and pushed it to github.  You can find that code here: <a href="https://github.com/scottTomaszewski/SafExample" rel="nofollow">https://github.com/scottTomaszewski/SafExample</a> and my interactions with the doc itself are in this class <a href="https://github.com/scottTomaszewski/SafExample/blob/master/app/src/main/java/com/example/safexample/SafDocument.java" rel="nofollow">https://github.com/scottTomaszewski/SafExample/blob/master/app/src/main/java/com/example/safexample/SafDocument.java</a></p>

<p>The error I am seeing when renaming a Uri acquired through google drive's SAF interface is this (easier to read version: <a href="https://github.com/scottTomaszewski/SafExample/blob/master/README.md" rel="nofollow">https://github.com/scottTomaszewski/SafExample/blob/master/README.md</a>)</p>

<pre><code>03-03 14:10:41.529 29069-29069/com.example.safexample W/grok: java.io.FileNotFoundException
        at android.database.DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(DatabaseUtils.java:144)
        at android.content.ContentProviderProxy.openTypedAssetFile(ContentProviderNative.java:692)
        at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1103)
        at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:942)
        at android.content.ContentResolver.openInputStream(ContentResolver.java:662)
        at com.example.safexample.SafDocument$2.openStream(SafDocument.java:66)
        at com.google.common.io.ByteSource$AsCharSource.openStream(ByteSource.java:420)
        at com.google.common.io.CharSource.read(CharSource.java:147)
        at com.example.safexample.SafDocument.read(SafDocument.java:73)
        at com.example.safexample.MainActivity.onOptionsItemSelected(MainActivity.java:61)
        at android.app.Activity.onMenuItemSelected(Activity.java:2912)
        at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:404)
        at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:167)
        at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:100)
        at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:100)
        at android.support.v7.app.ToolbarActionBar$2.onMenuItemClick(ToolbarActionBar.java:69)
        at android.support.v7.widget.Toolbar$1.onMenuItemClick(Toolbar.java:169)
        at android.support.v7.widget.ActionMenuView$MenuBuilderCallback.onMenuItemSelected(ActionMenuView.java:760)
        at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:811)
        at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
        at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:958)
        at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:948)
        at android.support.v7.widget.ActionMenuView.invokeItem(ActionMenuView.java:618)
        at android.support.v7.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:139)
        at android.view.View.performClick(View.java:5201)
        at android.view.View$PerformClick.run(View.java:21163)
        at android.os.Handler.handleCallback(Handler.java:746)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5443)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
03-03 14:10:41.535 29069-29069/com.example.safexample D/AndroidRuntime: Shutting down VM
03-03 14:10:41.538 29069-29069/com.example.safexample E/AndroidRuntime: FATAL EXCEPTION: main
  Process: com.example.safexample, PID: 29069
  java.lang.SecurityException: Permission Denial: reading com.google.android.apps.docs.storagebackend.StorageBackendContentProvider uri content://com.google.android.apps.docs.storage/document/renamed1457032241419.txt from pid=29069, uid=10054 requires android.permission.MANAGE_DOCUMENTS, or grantUriPermission()
        at android.os.Parcel.readException(Parcel.java:1599)
        at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
        at android.database.DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(DatabaseUtils.java:146)
        at android.content.ContentProviderProxy.openTypedAssetFile(ContentProviderNative.java:692)
        at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1103)
        at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:942)
        at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:865)
        at com.example.safexample.SafDocument$3.openStream(SafDocument.java:88)
        at com.google.common.io.ByteSource$AsCharSource.openStream(ByteSource.java:420)
        at com.google.common.io.CharSource.read(CharSource.java:147)
        at com.example.safexample.SafDocument.tryAsFileDescriptor(SafDocument.java:91)
        at com.example.safexample.SafDocument.read(SafDocument.java:77)
        at com.example.safexample.MainActivity.onOptionsItemSelected(MainActivity.java:61)
        at android.app.Activity.onMenuItemSelected(Activity.java:2912)
        at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:404)
        at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:167)
        at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:100)
        at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:100)
        at android.support.v7.app.ToolbarActionBar$2.onMenuItemClick(ToolbarActionBar.java:69)
        at android.support.v7.widget.Toolbar$1.onMenuItemClick(Toolbar.java:169)
        at android.support.v7.widget.ActionMenuView$MenuBuilderCallback.onMenuItemSelected(ActionMenuView.java:760)
        at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:811)
        at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
        at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:958)
        at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:948)
        at android.support.v7.widget.ActionMenuView.invokeItem(ActionMenuView.java:618)
        at android.support.v7.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:139)
        at android.view.View.performClick(View.java:5201)
        at android.view.View$PerformClick.run(View.java:21163)
        at android.os.Handler.handleCallback(Handler.java:746)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5443)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
</code></pre>

<p>I have tried various permutations of calling </p>

<pre><code>final int takeFlags = data.getFlags()
            &amp; (Intent.FLAG_GRANT_READ_URI_PERMISSION
            | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
    getContentResolver().takePersistableUriPermission(originalUri, takeFlags);
</code></pre>

<p>and</p>

<pre><code>&lt;uses-permission android:name="android.permission.MANAGE_DOCUMENTS" /&gt;
</code></pre>

<p>and </p>

<pre><code>Context#grantUriPermission(package, contentUri, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
</code></pre>

<p>I should also mention that I can of course execute the rename by using the ACTION-CREATE_DOCUMENT to make a new doc, copying the content of the old doc, and deleting the old doc, but this is not ideal nor do I want to go with a workaround.  This #renameDocument should work.  </p>

<p>Any help would be great!  </p>