简体   繁体   English

如果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 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.

相关问题 Android SQLite在打开时不存在错误,在运行时创建时已经存在错误 - Android SQLite doesn't exist error on open and already exist error on create during runtime 如果传递参数的表名在表中不存在,我们可以在firbase中自动创建数据库吗? android 工作室 - Can we create database automatically in the firbase if the table name with the passed parameter doesn't exist in the table ? android studio Android如果不存在python,如何创建一个新的json文件 - Android How to create a new json file if it doesn't exist python 启动 Android SDK 管理器 - 工具目录不存在? 苹果 - Launch Android SDK manager - Tools directory doesn't exist? Mac 缓存目录不存在 - Cache directory doesn't exist Android如何在启动时创建数据库(如果该数据库不存在),然后在下一次启动时检索它 - Android How to create database on Startup if it doesn't exist and then retrieve it on next Startups 如果 Android Gradle Build 中不存在默认属性文件,我该如何创建它 - How can I create a default properties file if it doesn't exist in Android Gradle Build Android编程的新功能-如果不存在则创建数据库 - New on android programming - create a database If it doesn't exist Gradle Android Maven插件不会自动创建pom文件 - Gradle Android Maven Plugin doesn't create the pom file automatically 如果不存在则插入整数,如果已存在于Firebase实时数据库Android中则增加整数 - Insert integer if doesn't exist, increment if already exists in Firebase realtime database Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM