我正在使用 android 的存储访问框架 (SAF) 与文档进行交互。我已经能够毫无问题地查找/读取/写入文档,但是当我尝试使用带有 Google Drive 文档的 DocumentsContract#renameDocument() 进行重命名时遇到了问题。我发布的代码适用于内部存储中的文件。
为了方便起见,我制作了一个示例应用程序并将其推送到 github。您可以在此处找到该代码:https ://github.com/scottTomaszewski/SafExample ,我与文档本身的交互在此类https://github.com/scottTomaszewski/SafExample/blob/master/app/src/main /java/com/example/safexample/SafDocument.java
我在重命名通过谷歌驱动器的 SAF 接口获取的 Uri 时看到的错误是这样的(更易于阅读的版本:https ://github.com/scottTomaszewski/SafExample/blob/master/README.md )
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)
我尝试了各种调用排列
final int takeFlags = data.getFlags()
& (Intent.FLAG_GRANT_READ_URI_PERMISSION
| Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
getContentResolver().takePersistableUriPermission(originalUri, takeFlags);
和
<uses-permission android:name="android.permission.MANAGE_DOCUMENTS" />
和
Context#grantUriPermission(package, contentUri, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
我还应该提一下,我当然可以通过使用 ACTION-CREATE_DOCUMENT 来创建新文档,复制旧文档的内容并删除旧文档来执行重命名,但这并不理想,我也不想使用一种解决方法。这个#renameDocument 应该可以工作。
任何帮助都会很棒!