我正在使用android的存储访问框架(SAF)与文档进行交互。我已经能够毫无困难地查找/读取/写入文档,但是当我尝试使用DocumentsContract#renameDocument()和Google Drive文档重命名时,我遇到了问题。我发布的代码可以很好地处理内部存储中的文件。
为了简单起见,我制作了一个示例应用程序并将其推送到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)
if(array[0] == address) Then
array[0] = 5; //Replaces the value of the address with a random integer.
else
array[0] *= 3; // Otherwise, if the element at index 0 is not an address, then multiply the number by an arbitrary integer.
我尝试了各种调用
的排列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" />
我还应该提一下,我当然可以通过使用ACTION-CREATE_DOCUMENT来创建一个新文档,复制旧文档的内容和删除旧文档来执行重命名,但这不是理想的,也不是我想要的采取一种解决方法。这#renameDocument应该可以工作。
任何帮助都会很棒!
答案 0 :(得分:0)
也许你应该试试DocumentFile.renameTo()。 顺便说一下,我目前在将/sdcard/1/something.txt重命名为/sdcard/1/app/sth/something.txt时遇到了重命名的问题,这是添加两级目录。你有什么想法吗?
答案 1 :(得分:-2)
这是由于文件操作是时间成本工作而你在主线程中做所有事情。建议是在单独的线程中执行文件操作工作,或者在另一个事件处理程序中单击处理菜单项。