作为I / O中的新手,我已经阅读并尝试使用this中的示例
this。但是,我只是无法在内部存储中创建一个简单的目录。一定是某个地方的错字。

对于我的应用程序,我已经使用以下file_paths.xml定义了FileProvider,用于存储应用程序内部文件。

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <files-path name="ma_pics" path="pictures/" />
</paths>


下面一个简单的方法应该提供一个位于“图片”目录中的文件,但是我的问题是,当我尝试创建目录时,该目录根本不存在。我究竟做错了什么?

@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);
    }

最佳答案

当我尝试创建目录时


您没有任何创建目录的代码。您正在创建一个File对象,该对象不会自动在文件系统上创建内容。在mkdirs()上调用picturesDir创建该目录。

09-30 15:17
查看更多