Android:外部存储上的 mkdirs()/mkdir() 返回 false
我对此感到疯狂:
Log.d("STATE", Environment.getExternalStorageState());
File f = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "work_data");
Log.d("PATH", f.getAbsolutePath());
if (!f.exists()) {
Log.d("MAKE DIR", f.mkdirs() + "");
}
输出日志如下所示:
STATE mounted
PATH /mnt/sdcard/DCIM/work_data
MAKE DIR false
我确保添加了正确的权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
但我不知道为什么它无法创建文件夹。我也一步步使用了mkdir()
,但结果是一样的。请帮我。我在谷歌上搜索了很多,并且在这个愚蠢的事情上花费了至少两天的时间。感谢您的帮助!!
编辑:
对不起大家!我在
标记中添加了
。这是我的错误!不过还是谢谢大家的回复。
I'm driven crazy with this:
Log.d("STATE", Environment.getExternalStorageState());
File f = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "work_data");
Log.d("PATH", f.getAbsolutePath());
if (!f.exists()) {
Log.d("MAKE DIR", f.mkdirs() + "");
}
The output log looks like this:
STATE mounted
PATH /mnt/sdcard/DCIM/work_data
MAKE DIR false
I made sure to add the correct permission:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
But I don't know why it could not create the folder. I also used mkdir()
step by step but the result is the same. Please help me. I have googled so much and spent at least 2 days on this stupid thing. Thanks for your help!!
EDITING:
Sorry everyone! I had added <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
in <application>
tag. this was my mistake! But thank you all for reply.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
我遇到了同样的问题,我确信我已经将权限标签放在了正确的位置,但是 mkdirs 还没有工作,我的系统是 Android 6.0,我现在解决了它,你可以检查如下:
I got the same problem,and I am sure I had put the permission tag in the right place,but mkdirs didn't work yet, my system is Android 6.0, I resolve it now , you can check as below:
我也遇到了同样的问题,我已经搜索了一周的所有内容试图找到答案。我想我找到了它,而且我认为它非常简单,您必须将使用权限声明放在正确的位置...
当我将它放在 中时 ,部分它不起作用。
I have had the same problem and I have searched everything for a week trying to find the answer. I think I found it and I think it's ridiculously easy, you have to put the uses-permission statement in the right place...
When I had it inside the <application></application> section it did not work.
仅当您的 targetSdkVersion 为 29 或更高时才适用
。Environment.getExternalStorageDirectory() 在 API 级别 29 中已弃用。
要获得相同的功能,请使用此行。
如果您已检查了所有可能的错误,请尝试此修复。
已经在这里回答 - https://stackoverflow.com/a/61480077/7764015
Applicable only if your targetSdkVersion is 29 or above
Environment.getExternalStorageDirectory() is deprecated in API level 29.
To get the same functionality use this line
If you have checked all the possible errors then try this fix.
Already answered here - https://stackoverflow.com/a/61480077/7764015
我知道这是一篇旧文章,但也许我的回答可以帮助别人。
经过几天处理这个问题后,我意识到,虽然手机连接到电脑(在开发过程中总是如此),但 SD 卡不可用。因此,在其上创建目录或文件的任何尝试都会失败。
为了使其“更容易”,它的行为似乎根据被测电话的不同而有所不同。
我知道这听起来是一个相当愚蠢的问题,但它花费了很多时间,也许其他人可以节省下来。
I know this is an old post but perhaps my answer can help somebody.
After several days dealing with this problem I have realized that while the phone is connected to the PC, which it turns to be always during development, the SD card is not available. Thus making to fail any attempt to create a directory or file over it.
To make it "easier" it seemed to behave differently depending of the telephone under test.
I know it can sound quite a silly problem, but it cost a lot of time that maybe some other can save.
在OnCreate()中添加这行代码
add this line of code in OnCreate()
可能现在回答已经太晚了,但是如果您已经允许 R/W 权限(也有运行时权限)并且仍然不起作用,请在您的 AndroidManifest.xml 中添加下面提到的行
注意:这如果您的目标是 Android 10+,则必须为必填项
Might be it's too late for answer but if you already allowed R/W permission(Runtime Permission too) and still doesn't work add this below mentioned line in your AndroidManifest.xml
Note: this must required if you'r targeting Android 10+
mkdirs() 仅当目录已创建时才返回 true。如果它已经就位,它应该返回 false。所以我敢打赌这个目录已经存在。
mkdirs() only returns true if the directory was created. If it is in place already, it should return false. So I would bet this directory already exists.
工作顺利。
work well.
我遇到了同样的问题,我只是想分享我的解决方案。
根据安卓
从 Android 4.4 开始,在应用的私有目录中读取或写入文件不需要 READ_EXTERNAL_STORAGE 或 WRITE_EXTERNAL_STORAGE 权限。
因此,您可以声明仅在较低版本的 Android 上才应请求该权限:添加 maxSdkVersion 属性:
I had the same issue, and I just wanted to share my fix.
as per android
Beginning with Android 4.4, reading or writing files in your app's private directories does not require the READ_EXTERNAL_STORAGE or WRITE_EXTERNAL_STORAGE permissions.
So you can declare the permission should be requested only on the lower versions of Android by adding the maxSdkVersion attribute:
把这个
代替
或尝试这个
put this
instead of
or try this
你的代码对我来说工作得很好。你的代码没有任何问题。
如果您要创建一个目录,请使用
f.mkdir()
而不是f.mkdirs()
您可以在
mnt => 中看到您的文件夹SD卡=> DCIM=>工作数据
Your code is working fine for me.There is no any thing wrong in your code.
Just if you are making one directory use
f.mkdir()
instead off.mkdirs()
You can see your folder in
mnt => Sd card => DCIM => work_data
如果你有android 6.0及以上版本,请确保你的gradle文件中的目标sdk小于22:
否则你需要实现运行时权限。
If you have android 6.0 and more, make sure your target sdk in gradle file less than 22:
or you need to implement run time permissions.
这可能会对将来的某人有所帮助。
问题也可能是你写了地址“/File/Address/”,而不是你应该写
这是一件小而愚蠢的事情,但在浪费了我几个小时的时间后,这对我有用。
This might help someone in the future.
the problem might also be that you write the address "/File/Address/" instead you should write
This is such a small and stupid thing but this worked for me after wasting hours of my time.
如果您的目标是 Android api 29,则将其添加到您的 AndroidManifest.xml
If you're targeting Android api 29 then add this in your AndroidManifest.xml
<application android:requestLegacyExternalStorage="true">
当 mkdirs() 不断返回 false 时,我尝试在外部存储“Test/Test1/Test2”中创建一组子文件夹。事实证明,“Test”是外部存储根目录中文件夹的保留字。这是在 Motorola G (XT1541) 上进行的,也可能适用于其他设备。
I tried to make a set of subfolders in external storage 'Test/Test1/Test2' when mkdirs() kept returning false. Turns out that 'Test' is either a reserved word for a folder in the external storage root. This was on a Motorola G (XT1541) and may apply to other devices as well.
对于新设备,您必须征求用户许可。
您可以在创建文件之前这样做:
在用户操作之后,您可以处理结果。
For new devices, you have to ask user permission.
You can do that like this before create your file:
After user's action, you can handle result.