首页 > 解决方案 > 我无法在外部存储中为我的应用创建文件夹

问题描述

一开始,代码运行良好。但是,当我从内部存储中删除该文件以测试它停止工作的代码时,每次它都从mkdirs()函数返回“ false ”。

我重新安装了几次应用程序,清除了缓存,但仍然无法正常工作。我还授予了在外部存储中写入的权限。

File direct = new File(Environment.getExternalStorageDirectory()+"/My App/Media/Images/Saved Images");

if (!direct.exists()) {
    boolean b=direct.mkdirs();
    System.out.println("Value"+b);
}

标签: androidfilemkdirs

解决方案


好的,我找到了解决方案。在 API 29 中,不推荐使用 getExternalStorageDirectory()。而不是我使用的

File direct=new File(Objects.requireNonNull(getApplicationContext().getExternalFilesDir(null)).getAbsolutePath()+"/My App/Media/Images/Saved Images");

推荐阅读