【发布时间】:2017-08-06 15:33:12
【问题描述】:
我是 android 开发的新手。 我想在我的外部 SD 卡中创建一个文件。我搜索了很多并尝试了不同的代码。但它是在设备存储中创建的,或者根本不创建。 我得到了我的 SD 卡路径,它在 /mnt/extSdCard 中。 我什至尝试了几个代码,但没有一个不起作用。 此代码找到了我的 SD 卡位置:
public static String getExternalSdCardPath() {
String path = null;
File sdCardFile = null;
List<String> sdCardPossiblePath = Arrays.asList("external_sd", "ext_sd", "external", "extSdCard");
for (String sdPath : sdCardPossiblePath) {
File file = new File("/mnt/", sdPath);
if (file.isDirectory() && file.canWrite()) {
path = file.getAbsolutePath();
Log.i("LOG", "path is: " + path);
String timeStamp = new SimpleDateFormat("ddMMyyyy_HHmmss").format(new Date());
File testWritable = new File(path, "test_" + timeStamp);
if (testWritable.mkdirs()) {
testWritable.delete();
} else {
path = null;
}
}
}
if (path != null) {
sdCardFile = new File(path);
} else {
sdCardFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
}
return sdCardFile.getAbsolutePath();
}
路径是我的位置。
【问题讨论】:
-
您是否检查并请求权限?在saving files 抢夺战利品
-
是的,当然。它是 WRITE_EXTERNAL_STORAGE
-
它在设备存储中创建,但在 extSdCard 中写入失败。它对我不起作用@ValiaSergeev
标签: android file storage android-sdcard sd-card