Android:外部存储上的 mkdirs()/mkdir() 返回 false [英] Android: mkdirs()/mkdir() on external storage returns false

查看:189
本文介绍了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

我确保添加了正确的权限:

I made sure to add the correct permission:

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 

但我不知道为什么它无法创建文件夹.我也一步一步使用了 mkdir() 但结果是一样的.请帮我.我在谷歌上搜索了很多,在这个愚蠢的事情上至少花了 2 天.感谢您的帮助!!

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!!

对不起大家!我在 标签中添加了 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> .这是我的错误!不过谢谢大家的回复.

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.

推荐答案

我遇到了同样的问题,我已经搜索了一周的时间试图找到答案.我想我找到了它,而且我认为它非常简单,您必须将使用权限声明放在正确的位置...

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...

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.company.name" 
    android:versionCode="1"
    android:versionName="0.2">

    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

当我把它放在 <application></application>部分它不起作用.

When I had it inside the <application></application> section it did not work.

这篇关于Android:外部存储上的 mkdirs()/mkdir() 返回 false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆