android mkdirs在SD卡上返回false

时间:2016-01-29 14:22:13

标签: java android

我在我的应用程序中创建了一个小文件浏览器,我想支持创建新文件夹。

当导航到设备内置内存中的某个位置时,一切都按预期工作,但当我导航到我的SD卡并尝试创建新文件夹时,行new File(path + File.separator + newFolderName).mkdirs()返回false。

我已经进行了以下调试测试:

String path = "/storage/external_SD";   // Not hard coded - user navigates here
new File(path).isDirectory();            --> true
new File(path).canRead();                --> true
new File(path).canWrite();               --> true

String newFolderPath = path + File.separator + newFolderName;
new File(newFolderPath).isDirectory();   --> false
new File(newFolderPath).mkdir();         --> false
new File(newFolderPath).mkdirs();        --> false

奇怪的是我注意到:我在mkdirs()的第一行的File类中放了一个断点,但应用程序没有停在该行上并立即返回false。这让我觉得即使我在正确的位置拥有正确的权限,也是一个权限问题:

<manifest ...>

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

    <application ...>
    ...
    </application>

</manifest>

我错过了什么?

1 个答案:

答案 0 :(得分:1)

自Android 5.0起,应用程序可能只会在外部SD卡上写入其私人位置。

对于所有其他位置,它需要实现document provider

Google documentation概述了访问外部存储的策略。