[英]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 directory path is not valid! abc目录路径无效! Please set the image directory name AppConstant.java class 请设置图像目录名AppConstant.java类
Please visit the following link to see the entire tutorial's code as I am using the same code: 请访问以下链接以查看整个教程的代码,因为我使用相同的代码:
http://www.androidhive.info/2013/09/android-fullscreen-image-slider-with-swipe-and-pinch-zoom-gestures/ http://www.androidhive.info/2013/09/android-fullscreen-image-slider-with-swipe-and-pinch-zoom-gestures/
I found this code in Utils
Class: 我在Utils
Class中找到了这段代码:
else { // image directory is empty Toast.makeText( _context, AppConstant.PHOTO_ALBUM + " is empty. Please load some images in it !", Toast.LENGTH_LONG).show(); else {//图像目录为空Toast.makeText(_context,AppConstant.PHOTO_ALBUM +“为空。请加载一些图像!”,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? 如何以编程方式创建缺少的目录而不是显示此错误对话框?
Here's how you create directories if they don't exist. 如果目录不存在,您可以在此创建目录。 Considering that directory
is indeed a directory. 考虑到该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()
will also create missing parent directories (ie all directories that lead to directory
). mkdirs()
还将创建缺少的父目录(即通向directory
所有directory
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.