Android无法创建目录
我无法创建目录,我拥有所有的权限,并且在我的清单中具有所有权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
在Main Activity otCreate中,检查权限,如果它具有创建目录,但它总是返回一个错误:
if (!checkPermission()) requestPermission();
else {
File folder = new File(Environment.getExternalStorageDirectory() +
File.separator + "receipts");
if (!folder.exists()) {
boolean bool = folder.mkdirs();
System.out.println(bool);
}
}
是否有任何线索或提示为什么?谢谢
I cannot create directory, I have all the permissions and this in my Manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
In MainActivity onCreate, checks permission, if it has it should create a directory but it always returns a false:
if (!checkPermission()) requestPermission();
else {
File folder = new File(Environment.getExternalStorageDirectory() +
File.separator + "receipts");
if (!folder.exists()) {
boolean bool = folder.mkdirs();
System.out.println(bool);
}
}
Any clue or hint to why? Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不幸的是,正如@Commonsware所说,随着Android 11及以上的安全更新,您根本无法在外部存储(SDCARD)上编写目录。
直接从 docs:
另外来自同一地方:
您的应用程序生成的系统目录可以存储任何信息。当然,这是有道理的,因为在任何给定时间,用户可以删除/格式化设备内的SD卡,并且您的应用程序的数据将完全丢失。
来自更多docs:
您会用这句话来编写:
和阅读文件:
这些都在您应用程序的“沙箱”文件夹中。因此,您无需声明权限。
Unfortunately, with the security updates brought by Android 11 and up, as @CommonsWare said, you simply can't write directories on external storage (sdcard).
Straight from the docs:
Additionally from the same place:
Your app has a system generated directory to store any information. This makes sense, of course, because at any given time the user could remove/format the sd card inside the device, and your app's data would be entirely lost.
From more docs:
You would use this to write:
And to read a file:
These are both within your app's "sandbox" folder. Because of this, you do not need to declare permissions.
更改为:
Change to: