【发布时间】:2016-08-11 23:40:15
【问题描述】:
我对内部存储没有问题,但我想在外部 SD 卡上创建一个目录,使用 KitKat 之前的版本,File.mkdirs() 有效。
我的代码中有一部分:
//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 卡进行测试。
更新:这就是我获取卷列表的方式:
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 卡中创建了目录:“test”。当我执行这段代码时:
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() 而不是直接指向。检查这个 - >stackoverflow.com/questions/32055815/…
-
无错误,无异常。 Environment.getExternalStorageDirectory.getPath() 返回“/storage/emulated/0/”,这不是外部sdcard
标签: android android-5.0-lollipop android-sdcard