安卓:无法创建默认图片文件夹目录 [英] Android: unable to create a directory in default pictures folder

查看:765
本文介绍了安卓:无法创建默认图片文件夹目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是code我使用创建的默认文件夹的图片文件夹:

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");
}

在日志中我得到:

False

False

在第1次目录不是present,因此,但随即我使用 mkdirs()<创建它/ code>,因此,我希望第二个日志是但即使是和我的应用程序崩溃因为 NullPointerException异常在code的后期

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

请帮忙

感谢您

推荐答案

您正在使用 Environment.DIRECTORY_PICTURES 走错了路。这只是一个字符串图片而不是一个路径不变。你需要得到通过的路径<一个href=\"http://developer.android.com/reference/android/os/Environment.html#getExternalStoragePublicDirectory%28java.lang.String%29\"相对=nofollow> Environment.getExternalStoragePublicDirectory(串)

You are using Environment.DIRECTORY_PICTURES the wrong way. It's just a String constant like "Pictures" but not a path. You need to get the path via Environment.getExternalStoragePublicDirectory(string)

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

这篇关于安卓:无法创建默认图片文件夹目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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