为什么所选文件的父路径不可用?
Why is the parent path of a selected file not usable?
以下代码用于启动文件管理器以允许用户select 文件:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
startActivityForResult(intent, REQUEST_CODE_CUSTOM_FOLDER);
如果用户select的文件foo.txt在文件夹Download中,返回的文件路径为
/document/2071-13DB:Download/foo.txt
读取那个文件没有问题。当应用程序尝试将文本文件 (foo_new.txt) 写入文件夹“/document/2071-13DB:Download”时,出现以下异常:
Error:Directory '/document/2071-13DB:Download' could not be created.
有人能解释一下吗?我试图让用户 select 应用程序的文件夹来保存文件。
The following code is used to launch a file manager to allow the user to select a file
不,不是。它用于从支持特定 Intent
结构的任何 activity 中获取一段内容。
If the user selects file foo.txt in folder Download, the returned file's path is
您没有得到文件。您正在为一段内容获得 Uri
。特别是,我强烈怀疑这个 Uri
有一个 content
方案。那样的话,路径对你来说就没有意义了。
When the app tries to write a text file (foo_new.txt) to folder "/document/2071-13DB:Download", it gets the following exception
那不是文件夹。从你的角度来看,那是一系列随机字符。
比如这个网页的URL是 HTTP 客户端读取该页面的内容。但是,如果您尝试使用 Java 的 File
对象将新文件写入计算机上的 /questions/39337012/
,您会发现这不会起作用,因为您没有 /questions/39337012/
文件夹在您的计算机上。在这里,URL 指向不在您计算机文件系统上的内容。
同样,您不能对 Uri
的一部分执行字符串操作并想出另一个可以使用的 Uri
。 Uri
不必指向文件系统上的文件。
I am trying to allow the user to select a folder for the app to save files.
使用a directory-chooser library,然后。
或者,在 Android 5.0+ 设备上,使用 ACTION_OPEN_DOCUMENT_TREE
,它是存储访问框架的一部分。从那里,使用 DocumentFile
创建新文件夹,在这些文件夹中创建新内容等,所有这些都使用 Uri
值而不试图破译 Uri
值的片段(例如,getPath()
).
以下代码用于启动文件管理器以允许用户select 文件:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
startActivityForResult(intent, REQUEST_CODE_CUSTOM_FOLDER);
如果用户select的文件foo.txt在文件夹Download中,返回的文件路径为
/document/2071-13DB:Download/foo.txt
读取那个文件没有问题。当应用程序尝试将文本文件 (foo_new.txt) 写入文件夹“/document/2071-13DB:Download”时,出现以下异常:
Error:Directory '/document/2071-13DB:Download' could not be created.
有人能解释一下吗?我试图让用户 select 应用程序的文件夹来保存文件。
The following code is used to launch a file manager to allow the user to select a file
不,不是。它用于从支持特定 Intent
结构的任何 activity 中获取一段内容。
If the user selects file foo.txt in folder Download, the returned file's path is
您没有得到文件。您正在为一段内容获得 Uri
。特别是,我强烈怀疑这个 Uri
有一个 content
方案。那样的话,路径对你来说就没有意义了。
When the app tries to write a text file (foo_new.txt) to folder "/document/2071-13DB:Download", it gets the following exception
那不是文件夹。从你的角度来看,那是一系列随机字符。
比如这个网页的URL是File
对象将新文件写入计算机上的 /questions/39337012/
,您会发现这不会起作用,因为您没有 /questions/39337012/
文件夹在您的计算机上。在这里,URL 指向不在您计算机文件系统上的内容。
同样,您不能对 Uri
的一部分执行字符串操作并想出另一个可以使用的 Uri
。 Uri
不必指向文件系统上的文件。
I am trying to allow the user to select a folder for the app to save files.
使用a directory-chooser library,然后。
或者,在 Android 5.0+ 设备上,使用 ACTION_OPEN_DOCUMENT_TREE
,它是存储访问框架的一部分。从那里,使用 DocumentFile
创建新文件夹,在这些文件夹中创建新内容等,所有这些都使用 Uri
值而不试图破译 Uri
值的片段(例如,getPath()
).