我正在尝试在 Android 上创建一个简单的图像文件,并具有以下两种方法: 创建目录:
private void createThumbnailDir() {
File file = new File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
+ File.separator + "scouthouse",
"scouthouse_thumbnails");
this.getActivity().sendBroadcast(
new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"
+ Environment.getExternalStorageDirectory())));
if (!file.mkdirs()) {
Log.d("file", "file not created");
}
}
创建文件:
private File createNewThumbnailFile() {
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy-HH-mm-ss",
Locale.ENGLISH);
Date date = Calendar.getInstance().getTime();
File file = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
"scouthouse" + File.separator + "schouthouse_thumbnails" + File.separator
+ sdf.format(date) + ".jpg");
try {
if (file.createNewFile()) {
return file;
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
当我创建文件时,会引发以下 IOException:java.io.IOException: ENOENT (No such file or directory)
但是当我检查手机上的文件管理器时,该目录确实存在。
编辑:
有关错误的更多信息:stacktrace = null,所以我只有原因和详细信息,它们都是相同的:libcore.io.ErrnoException:打开失败:ENOENT(没有这样的文件或目录)