Android: 无法检索文件夹的 URI
Android: Unable to retrieve the Uri of a folder
我有一个应用程序,它允许用户通过 Android 文件选择器 Intent.ACTION_OPEN_DOCUMENT_TREE
选择一个文件夹,然后在选定的文件夹中创建一个 ZIP 文件,该文件最终将被共享。
我可以通过 OnActivityResult 函数中的 Uri uri = data.getData();
检索选定的文件夹 Uri。
这有效,我选择的 SD 卡的示例 Uri 如下所示:content://com.android.externalstorage.documents/tree/15E6-0F1A%3A
现在我需要在应用程序缓存文件夹中临时存储一个 ZIP 文件,然后再将其共享出去。但是我很难找到缓存文件夹的 Content Uri?
我认为我需要做的是:
File filePath = new File(this.getCacheDir(), "");
Uri uri = FileProvider.getUriForFile(this, this.getApplicationContext().getPackageName() + ".provider", filePath);
并将 Uri 发送到创建和共享 ZIP 文件的函数,但使用文件夹调用 FileProvider.getUriForFile
会导致应用程序崩溃。 java.lang.StringIndexOutOfBoundsException
只要我在缓存文件夹中创建一个文件并使用这个文件句柄调用 FileProvider.getUriForFile
,我就会得到一个正确的 Uri:
File filePath = new File(this.getCacheDir(), "");
File newFile = new File(filePath, "tmpFileName");
Uri uri = FileProvider.getUriForFile(this, this.getApplicationContext().getPackageName() + ".provider", newFile);
这么看来 FileProvider.getUriForFile 无法处理文件夹?
我上下搜索,但不知何故,我发现的所有内容都是关于文件的,我需要一个文件夹 Uri。我已经尝试过像 DocumentFile 和 File 这样的替代品,但我最终得到的只是 "file" Uris' 而不是 "content" Uris.
因此我的问题是,有没有人知道从文件夹中获取内容 URI 的方法,或者更具体地说,如何获取 content Uri this.getCacheDir()
?
I can retrieve the selected folder Uri via Uri uri = data.getData(); in the OnActivityResult function.
那不是文件夹。它是一个文档树。请特别注意,它不必代表文件系统上的目录。
Now I need to temporarily store a ZIP file in the applications cache folder, before sharing it out
在 getCacheDir()
中创建指向所需目标文件的 File
对象,然后使用标准 Java 文件 I/O 复制或创建您的 ZIP 文件 File
对象。请注意,此操作不需要 Uri
值。
So it seems like FileProvider.getUriForFile can't handle folders?
正确。
And the send the Uri on to the function which creates and shares the ZIP file
您共享的 Uri
将指向 ZIP 文件。您不需要以某种方式指向 getCacheDir()
的 Uri
。您可以使用普通文件 I/O 来创建 ZIP 文件,而不涉及 Uri
.
how can I get the content Uri of this.getCacheDir()?
Android 中没有任何内容真正支持这个概念,您也不需要它。
我有一个应用程序,它允许用户通过 Android 文件选择器 Intent.ACTION_OPEN_DOCUMENT_TREE
选择一个文件夹,然后在选定的文件夹中创建一个 ZIP 文件,该文件最终将被共享。
我可以通过 OnActivityResult 函数中的 Uri uri = data.getData();
检索选定的文件夹 Uri。
这有效,我选择的 SD 卡的示例 Uri 如下所示:content://com.android.externalstorage.documents/tree/15E6-0F1A%3A
现在我需要在应用程序缓存文件夹中临时存储一个 ZIP 文件,然后再将其共享出去。但是我很难找到缓存文件夹的 Content Uri?
我认为我需要做的是:
File filePath = new File(this.getCacheDir(), "");
Uri uri = FileProvider.getUriForFile(this, this.getApplicationContext().getPackageName() + ".provider", filePath);
并将 Uri 发送到创建和共享 ZIP 文件的函数,但使用文件夹调用 FileProvider.getUriForFile
会导致应用程序崩溃。 java.lang.StringIndexOutOfBoundsException
只要我在缓存文件夹中创建一个文件并使用这个文件句柄调用 FileProvider.getUriForFile
,我就会得到一个正确的 Uri:
File filePath = new File(this.getCacheDir(), "");
File newFile = new File(filePath, "tmpFileName");
Uri uri = FileProvider.getUriForFile(this, this.getApplicationContext().getPackageName() + ".provider", newFile);
这么看来 FileProvider.getUriForFile 无法处理文件夹?
我上下搜索,但不知何故,我发现的所有内容都是关于文件的,我需要一个文件夹 Uri。我已经尝试过像 DocumentFile 和 File 这样的替代品,但我最终得到的只是 "file" Uris' 而不是 "content" Uris.
因此我的问题是,有没有人知道从文件夹中获取内容 URI 的方法,或者更具体地说,如何获取 content Uri this.getCacheDir()
?
I can retrieve the selected folder Uri via Uri uri = data.getData(); in the OnActivityResult function.
那不是文件夹。它是一个文档树。请特别注意,它不必代表文件系统上的目录。
Now I need to temporarily store a ZIP file in the applications cache folder, before sharing it out
在 getCacheDir()
中创建指向所需目标文件的 File
对象,然后使用标准 Java 文件 I/O 复制或创建您的 ZIP 文件 File
对象。请注意,此操作不需要 Uri
值。
So it seems like FileProvider.getUriForFile can't handle folders?
正确。
And the send the Uri on to the function which creates and shares the ZIP file
您共享的 Uri
将指向 ZIP 文件。您不需要以某种方式指向 getCacheDir()
的 Uri
。您可以使用普通文件 I/O 来创建 ZIP 文件,而不涉及 Uri
.
how can I get the content Uri of this.getCacheDir()?
Android 中没有任何内容真正支持这个概念,您也不需要它。