如何自动创建Android目录(如果尚不存在) [英] How to create Android directory automatically if it doesn't already exist

查看:167
本文介绍了如何自动创建Android目录(如果尚不存在)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用教程创建画廊应用,但出现以下错误:

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:

http ://www.androidhive.info/2013/09/android-fullscreen-image-slider-with-swipe-and-pinch-zoom-gestures/

我在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆