我在我的应用程序中创建了一个小文件资源管理器,并且我想支持创建新文件夹。android mkdirs在SD卡上返回false
导航到设备内置内存中的位置时,一切都按预期工作,但是当我导航到我的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()
第一线,但应用程序没有对停止并立即返回false。这让我觉得这是一个权限问题,即使我在正确的位置有正确的权限:
<manifest ...>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application ...>
...
</application>
</manifest>
我在想什么?
只是一个问题,我不确定,你的路径是'/ storage/external_SD /'正确吗?现在你的newFolderPath等于'path + File.separator + newFolderName'。您的路径末尾已有分隔符,并使用File.separator插入另一个分隔符。这可能是问题吗? –
@GeorgeD我的不好,路径字符串没有最后一个分隔符。我修复了这个问题 – iMax531
你可以发布newFolderPath的值吗? –