我在我的应用程序中制作了一个小型文件浏览器,希望支持创建新文件夹。
当导航到设备内置存储器中的位置时,一切正常,但当我导航到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
我注意到一些奇怪的事情:我在 File 类中在 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插入另一个分隔符。这可能是问题所在吗? - George Daramouskas