首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在android外部存储目录中创建文件夹?

如何在android外部存储目录中创建文件夹?
EN

Stack Overflow用户
提问于 2014-07-16 20:47:47
回答 8查看 65K关注 0票数 38

我无法在android外部存储目录中创建文件夹。

我在载货单上加上了遗漏,

代码语言:javascript
复制
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

下面是我的代码:

代码语言:javascript
复制
 String Path = Environment.getExternalStorageDirectory().getPath().toString()+ "/Shidhin/ShidhiImages";
  System.out.println("Path  : " +Path );
  File FPath = new File(Path);
  if (!FPath.exists()) {
        if (!FPath.mkdir()) {
            System.out.println("***Problem creating Image folder " +Path );
        }
  }
EN

回答 8

Stack Overflow用户

回答已采纳

发布于 2014-07-16 20:54:00

如下所示:

代码语言:javascript
复制
String folder_main = "NewFolder";

File f = new File(Environment.getExternalStorageDirectory(), folder_main);
if (!f.exists()) {
    f.mkdirs();
}

如果您想在其中创建另一个文件夹:

代码语言:javascript
复制
File f1 = new File(Environment.getExternalStorageDirectory() + "/" + folder_main, "product1");
if (!f1.exists()) {
    f1.mkdirs();
}
票数 77
EN

Stack Overflow用户

发布于 2014-07-16 20:51:06

mkdirmkdirs之间的区别在于,mkdir不会创建不存在的父目录,而mkdirs会创建父目录,因此如果Shidhin不存在,mkdir将失败。此外,仅当创建了目录时,mkdirmkdirs才返回true。如果该目录已经存在,则返回false

票数 13
EN

Stack Overflow用户

发布于 2020-02-27 00:39:17

现在不建议使用Environment.getExternalStorageDirectory(),因为API级别29,该选项使用:

Context.getExternalFilesDir()

示例:

代码语言:javascript
复制
void createExternalStoragePrivateFile() {
    // Create a path where we will place our private file on external
    // storage.
    File file = new File(getExternalFilesDir(null), "DemoFile.jpg");

    try {
        // Very simple code to copy a picture from the application's
        // resource into the external file.  Note that this code does
        // no error checking, and assumes the picture is small (does not
        // try to copy it in chunks).  Note that if external storage is
        // not currently mounted this will silently fail.
        InputStream is = getResources().openRawResource(R.drawable.balloons);
        OutputStream os = new FileOutputStream(file);
        byte[] data = new byte[is.available()];
        is.read(data);
        os.write(data);
        is.close();
        os.close();
    } catch (IOException e) {
        // Unable to create file, likely because external storage is
        // not currently mounted.
        Log.w("ExternalStorage", "Error writing " + file, e);
    }
}

void deleteExternalStoragePrivateFile() {
    // Get path for the file on external storage.  If external
    // storage is not currently mounted this will fail.
    File file = new File(getExternalFilesDir(null), "DemoFile.jpg");
    file.delete();
}

boolean hasExternalStoragePrivateFile() {
    // Get path for the file on external storage.  If external
    // storage is not currently mounted this will fail.
    File file = new File(getExternalFilesDir(null), "DemoFile.jpg");
    return file.exists();
}
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24781213

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档