作为 I/O 的新手,我已经阅读并尝试使用 this 中的示例。 和 this 。但是,我无法在内部存储中创建简单的目录。肯定是某个地方写错了。
对于我的应用程序,我使用以下 file_paths.xml 定义了 FileProvider,用于存储应用程序内部文件。
<?xml version="1.0" encoding="utf-8"?>
<paths>
<files-path name="ma_pics" path="pictures/" />
</paths>
下面的一个简单方法应该提供一个驻留在“pictures”目录中的文件,但我的问题是当我尝试创建目录时,该目录根本不存在。我做错了什么?
@Nonnull
public final File providePictureFile(@Nonnull String prefix, @Nonnull String suffix)
throws IOException {
File picturesDir = new File(mContext.getFilesDir(), "pictures");
if(!picturesDir.exists()){
// code forks here, dir doesn't exist, throws the following exception
throw new IOException("Pictures dir doesn't exist");
}
return new File(picturesDir, prefix + suffix);
}
最佳答案
when I try to create the a directory
您没有任何创建目录的代码。您正在创建一个 File
对象,该对象不会自动在文件系统上创建某些内容。对 picturesDir
调用 mkdirs()
以创建该目录。
关于java - context.getFilesDir() 时文件目录不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49341072/