我對內部存儲沒有任何問題,但我想在外部SD卡上創建一個目錄,其版本在KitKat之前的版本爲File.mkdirs()
。無法在外部SD卡(棒棒糖)中創建目錄
有我的部分代碼:
//external sd card
DocumentFile.fromFile(new File("/storage/sdcard1/")).exists(); //returns true
DocumentFile.fromFile(new File("/storage/sdcard1/")).canRead(); //returns true
DocumentFile.fromFile(new File("/storage/sdcard1/")).canWrite(); //returns true
DocumentFile.fromFile(new File("/storage/sdcard1/test")).exists(); //returns false
DocumentFile.fromFile(new File("/storage/sdcard1/")).createDirectory("test"); //returns null
//internal storage
DocumentFile.fromFile(new File("/storage/emulated/0/")).exists(); //returns true
DocumentFile.fromFile(new File("/storage/emulated/0/test")).exists(); //returns false
DocumentFile.fromFile(new File("/storage/emulated/0/")).createDirectory("test"); //it works
DocumentFile.fromFile(new File("/storage/emulated/0/test")).exists(); //returns true
DocumentFile.fromFile(new File("/storage/emulated/0/test")).createFile("text", "file.txt"); //it works
//external sd card
(new File("/storage/sdcard1/test2/subfolder")).exists(); //returns false
(new File("/storage/sdcard1/test2/subfolder")).mkdirs(); //returns false
//internal storage
(new File("/storage/emulated/0/test2/subfolder")).exists(); //returns false
(new File("/storage/emulated/0/test2/subfolder")).mkdirs(); //returns true
在AndroidManifest.xml:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
測試與BQ Aquaris E4,運行Android Lollipop 5.0和1GB的micro SD卡。
UPDATE:這是我拿貨量列表:
private static ArrayList<StorageInfo> listAvaliableStorage(Context context) {
ArrayList<StorageInfo> storagges = new ArrayList<>();
StorageManager storageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
try {
Class<?>[] paramClasses = {};
Method getVolumeList = StorageManager.class.getMethod("getVolumeList", paramClasses);
getVolumeList.setAccessible(true);
Object[] params = {};
Object[] invokes = (Object[]) getVolumeList.invoke(storageManager, params);
if (invokes != null) {
StorageInfo info;
for (Object obj : invokes) {
Method getPath = obj.getClass().getMethod("getPath");
String path = (String) getPath.invoke(obj);
info = new StorageInfo(path);
File file = new File(info.getPath());
if ((file.exists()) && (file.isDirectory())
//&& (file.canWrite())
) {
info.setTotalStorage(file.getTotalSpace());
info.setFreeStorage(file.getUsableSpace());
Method isRemovable = obj.getClass().getMethod("isRemovable");
String state;
try {
Method getVolumeState = StorageManager.class.getMethod("getVolumeState", String.class);
state = (String) getVolumeState.invoke(storageManager, info.getPath());
info.setState(state);
info.setRemovable((Boolean) isRemovable.invoke(obj));
} catch (Exception e) {
e.printStackTrace();
}
storagges.add(info);
}
}
}
} catch (NoSuchMethodException | IllegalArgumentException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
storagges.trimToSize();
return storagges;
}
硬編碼路徑是唯一在這個例子中
更新2: 我創建的目錄:在「測試」帶有文件瀏覽器的SD卡。當我執行此代碼:
File file = new File("/storage/sdcard1/test/", "file.txt");
fileOutput = new FileOutputStream(file);
第二行拋出FileNotFoundException: /storage/sdcard1/test/file.txt: open failed: EACCES (Permission denied)
我在做什麼錯?
Logcat中返回任何錯誤消息? –
你也可以發佈你收到的錯誤嗎?另外,請嘗試使用Environment.getExternalStorageDirectory()。getPath()而不是直接指向。檢查此 - > http://stackoverflow.com/questions/32055815/android-mkdirs-not-working-for-me-not-external-storage – Techidiot
沒有錯誤,沒有例外。 Environment.getExternalStorageDirectory.getPath()返回「/ storage/emulated/0 /」,這不是外部sdcard – cgr