我为什么不能让里面Environment.DIRECTORY_PICTURES目录? [英] Why can i not make a directory inside Environment.DIRECTORY_PICTURES?

查看:1837
本文介绍了我为什么不能让里面Environment.DIRECTORY_PICTURES目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的code

File selfieLocation = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES),
               "Daily Selfies");
boolean isDirectory = false;
if(!selfieLocation.isDirectory()) {
        //Creates directory named by this file
        selfieLocation.mkdir();
        isDirectory = selfieLocation.isDirectory();
 }
 //array of strings 
 for(String selfiePath: selfieLocation.list()) {
        selfies.add(selfiePath);
 }

基本上我所试图做的是建立在其中放置是提供给用户图像的标准目录里面我自己的自定义目录。

Basically what I am trying to do is create my own customizable directory inside of the standard directory in which to place pictures that are available to the user.

我看了一下相关的线程,并看到这一个,<一个href=\"http://stackoverflow.com/questions/12598567/android-unable-to-create-a-directory-in-default-pictures-folder\">Android:无法创建默认的图片文件夹目录。不过,我确信,我不得不getExternal一个电话......,而不是仅仅有Environment.DIRECTORY_PICTURES作为参数。
我也看了放在这里 http://developer.android.com/指南/主题/数据/数据​​storage.html#filesExternal ,看到我有正确的方法调用/格式创建外部存储器可自定义的文件夹。该文档的例子就是

I looked at related threads and saw this one, Android: unable to create a directory in default pictures folder. However I made sure that I had a call to getExternal...., and not just have Environment.DIRECTORY_PICTURES as a parameter. I also looked on here http://developer.android.com/guide/topics/data/data-storage.html#filesExternal and saw that I had the right method call/format to create a customizable folder in external memory. The docs example was

File file = new File(Environment.getExternalStoragePublicDirectory(
        Environment.DIRECTORY_PICTURES), albumName);
if (!file.mkdirs()) {
    Log.e(LOG_TAG, "Directory not created");
}

我通过我的code阶梯,只见局部变量isDirectory甚至调用selfieLocation.mkdir()后留在假的。有谁知道为什么不能创建该目录?

I stepped through my code and saw that the local variable isDirectory stayed at false even after the call to selfieLocation.mkdir(). Does anyone know why this directory cannot be created?

推荐答案

尝试创建文件#mkdirs目录(),而不是文件# MKDIR()。后者假定所有的父目录已经到位,因此,如果任何父母都不存在它不会产生目录。结果
此外,参加的Andr​​oidManifest.xml 看看你的权限。您需要以下权限才能读/写外部存储器中的内容:

Try to create directory with File#mkdirs(), not File#mkdir(). The latter assumes that all parent directories are already in place, so it won't create directory if any of its parents don't exist.
Also, take a look at your permissions in AndroidManifest.xml. You need the following permissions in order to read/write the content on external storage:

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

android.permission.READ_EXTERNAL_STORAG​​E 不需要了,但的将在Android的未来版本中的。

这篇关于我为什么不能让里面Environment.DIRECTORY_PICTURES目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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