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

SHI*_*T.S 33 directory android

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

我在清单上添加了许可,

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

 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 );
        }
  }
Run Code Online (Sandbox Code Playgroud)

Dha*_*val 70

像这样做 :

String folder_main = "NewFolder";

File f = new File(Environment.getExternalStorageDirectory(), folder_main);
if (!f.exists()) {
    f.mkdirs();
}
Run Code Online (Sandbox Code Playgroud)

如果你想创建另一个文件夹:

File f1 = new File(Environment.getExternalStorageDirectory() + "/" + folder_main, "product1");
if (!f1.exists()) {
    f1.mkdirs();
}
Run Code Online (Sandbox Code Playgroud)

  • getExternalStorageDirectory 已弃用,您可以使用替代 getExternalFilesDir 或其他。 (3认同)
  • @Seyid-KananBagirov 请在给出答案时具体说明。当应用程序删除其数据时,``getExternalFilesDir`` 也会被清除。那么,“其他”方法有哪些呢? (2认同)

Bla*_*elt 11

mkdir和之间的区别mkdirsmkdir不会创建不存在的父目录,而mkdirs如果Shidhin不存在,mkdir则会失败.此外,mkdirmkdirs返回true只有在目录中创建.如果目录已存在,则返回false


小智 10

getexternalstoragedirectory()已被弃用。我找到了解决方案,可能对您有帮助。(这是 2021 年 6 月的解决方案)

对应包括Api 30、Android 11:

现在,使用此 commonDocumentDirPath 来保存文件。

步骤1

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Run Code Online (Sandbox Code Playgroud)

第2步

public  static  File commonDocumentDirPath(String FolderName){
    File dir = null ;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
        dir = new File (Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS)+ "/"+FolderName );
    } else {
        dir = new File(Environment.getExternalStorageDirectory() + "/"+FolderName);
    }   
    return  dir ;

}
Run Code Online (Sandbox Code Playgroud)


Jor*_*sys 5

从 API 级别 29 起,Environment.getExternalStorageDirectory()的使用现在已被弃用,该选项正在使用:

Context.getExternalFilesDir()

例子:

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();
}
Run Code Online (Sandbox Code Playgroud)