我在我的應用程序中創建了一個小文件資源管理器,並且我想支持創建新文件夾。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的值嗎? –