如何自动创建Android目录(如果尚不存在) [英] How to create Android directory automatically if it doesn't already exist
问题描述
我正在使用教程创建画廊应用,但出现以下错误:
I am creating a gallery app using a tutorial but get the following error:
abc目录路径无效!请设置图像目录名称AppConstant.java类
请访问以下链接,以查看我使用的相同代码的完整教程代码:
Please visit the following link to see the entire tutorial's code as I am using the same code:
我在Utils
类中找到了此代码:
I found this code in Utils
Class:
else { //图片目录为空 Toast.makeText( _语境, AppConstant.PHOTO_ALBUM +为空.请在其中加载一些图像!", Toast.LENGTH_LONG).show(); }
else { // image directory is empty Toast.makeText( _context, AppConstant.PHOTO_ALBUM + " is empty. Please load some images in it !", Toast.LENGTH_LONG).show(); }
} else {
AlertDialog.Builder alert = new AlertDialog.Builder(_context);
alert.setTitle("Error!");
alert.setMessage(AppConstant.PHOTO_ALBUM
+ " directory path is not valid! Please set the image directory name AppConstant.java class");
alert.setPositiveButton("OK", null);
alert.show();
}
return filePaths;
如何通过编程方式创建丢失的目录,而不是显示此错误对话框?
How can I create the missing directory programmatically instead of display this error dialog?
推荐答案
如果目录不存在,请按照以下方法创建目录.考虑到directory
确实是目录.
Here's how you create directories if they don't exist. Considering that directory
is indeed a directory.
// If the parent dir doesn't exist, create it
if (!directory.exists()) {
if (parentDir.mkdirs()) {
Log.d(TAG, "Successfully created the parent dir:" + parentDir.getName());
} else {
Log.d(TAG, "Failed to create the parent dir:" + parentDir.getName());
}
}
mkdirs()
还将创建丢失的父目录(即所有导致directory
的目录).
mkdirs()
will also create missing parent directories (i.e. all directories that lead to directory
).
这篇关于如何自动创建Android目录(如果尚不存在)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!