简体   繁体   English

Android:无法在默认图片文件夹中创建目录

[英]Android: unable to create a directory in default pictures folder

This is the code i am using to create a folder in the default pictures folder: 这是我用来在默认图片文件夹中创建文件夹的代码:

File imagesFolder = new File(Environment.DIRECTORY_PICTURES, "/images");
if (!imagesFolder.exists()) {
Log.d("if imagesFolder exists - 1", "False");
imagesFolder.mkdirs();
} else {
Log.d("if imagesFolder exists - 1", "True");
}
if (!imagesFolder.exists()) {
Log.d("if imagesFolder exists - 2", "False");
imagesFolder.mkdirs();
} else {
Log.d("if imagesFolder exists - 2", "True");
}

in log i am getting: 在日志我得到:

False

False

for the 1st time the directory is not present, hence False but then immediately i am creating it using mkdirs() , hence i expect the 2nd log to be True but even that is False and my application crashed because of NullPointerException in the later part of the code 第一次目录不存在,因此False但后来我立即使用mkdirs()创建它,因此我希望第二个日志为True但即使这是False ,我的应用程序崩溃,因为在后面的部分NullPointerException编码

Please help 请帮忙

Thank You 谢谢

You are using Environment.DIRECTORY_PICTURES the wrong way. 您正在以错误的方式使用Environment.DIRECTORY_PICTURES It's just a String constant like "Pictures" but not a path. 它只是一个String常量,如"Pictures"但不是路径。 You need to get the path via Environment.getExternalStoragePublicDirectory(string) 您需要通过Environment.getExternalStoragePublicDirectory(string)获取路径

File pictureFolder = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES
        );
File imagesFolder = new File(pictureFolder, "images");
// etc

To generate a folder at first you need to add permission at AndroidMinifest.xml 要首先生成文件夹,您需要在AndroidMinifest.xml中添加权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Now call following method to create a new folder with in which Directory you want to create your folder(this name should exist in Environment. list) and your folder name. 现在调用以下方法创建一个新文件夹,其中包含要在其中创建文件夹的目录(此名称应存在于环境。列表中)和文件夹名称。

File outputDirectory = GetPhotoDirectory(Environment.DIRECTORY_PICTURES, "YourFolder");

Generate your folder by this method 通过此方法生成您的文件夹

public static File GetDirectory(String inWhichFolder, String yourFolderName ) {
    File outputDirectory = null;

    String externalStorageState = Environment.getExternalStorageState();
    if (externalStorageState.equals(Environment.MEDIA_MOUNTED)) {

        File pictureDirectory = Environment.getExternalStoragePublicDirectory(inWhichFolder);

        outputDirectory = new File(pictureDirectory, yourFolderName);
        if (!outputDirectory.exists()) {
            if (!outputDirectory.mkdirs()) {
                Log.e(LogHelper.LogTag, "Failed to create directory: " + outputDirectory.getAbsolutePath());
                outputDirectory = null;
            }
        }
    }
    return outputDirectory;
}

If you want to create a file under your newly created folder then you can use below code 如果要在新创建的文件夹下创建文件,则可以使用下面的代码

public static Uri GenerateTimeStampPhotoFileUri(File outputDirectory, String fileName){
    Uri photoFileUri = null;

    if(outputDirectory!=null) {
        File photoFile = new File(outputDirectory, fileName);
        photoFileUri = Uri.fromFile(photoFile);
    }
    return photoFileUri;
}

Call to create a file after creating the folder with File Directory. 使用文件目录创建文件夹后调用创建文件。 It will return your file Uri 它将返回您的文件Uri

Uri fileUri = GenerateTimeStampPhotoFileUri(outputDirectory, fileName);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM