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

时间:2014-07-16 12:47:47

标签: android directory

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

我在清单上添加了许可,

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

这是我的代码:

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

9 个答案:

答案 0 :(得分:63)

这样做:

String folder_main = "NewFolder";

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

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

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

答案 1 :(得分:10)

mkdirmkdirs之间的区别在于mkdir不会创建不存在的父目录,而mkdirs会这样做,所以如果Shidhin不存在, mkdir将失败。此外,mkdirmkdirs仅在创建目录时才返回true。如果目录已存在,则返回false

答案 2 :(得分:4)

从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();
}

答案 3 :(得分:3)

我可以在android外部存储目录中创建一个文件夹。

我在清单上添加了允许,

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

这是我的代码:

String folder_main = "Images";

File outerFolder = new File(Environment.getExternalStorageDirectory(), folder_main);

File inerDire = new File(outerFolder.getAbsoluteFile(), System.currentTimeMillis() + ".jpg");

if (!outerFolder.exists()) {

    outerFolder.mkdirs();

}
if (!outerFolder.exists()) {

    inerDire.createNewFile();

}
  • outerFolder.mkdirs(); //这将创建一个文件夹

  • inerDire.createNewFile(); //这将创建文件(例如.jpg
    文件)

答案 4 :(得分:1)

我们可以在外部存储上创建文件夹或目录:

 String myfolder=Environment.getExternalStorageDirectory()+"/"+fname;
     File f=new File(myfolder);
     if(!f.exists())
     if(!f.mkdir()){
     Toast.makeText(this, myfolder+" can't be created.", Toast.LENGTH_SHORT).show();

    }
    else
     Toast.makeText(this, myfolder+" can be created.", Toast.LENGTH_SHORT).show();
}

如果我们想在内存上创建目录或文件夹,那么我们将:

File folder = getFilesDir(); 
File f= new File(folder, "doc_download"); 
f.mkdir();

但是确保你已经给出了写外部存储许可。 请记住,如果您没有外部驱动器,则它默认选择内部父目录。 我肯定会工作.....享受代码

答案 5 :(得分:1)

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

对应于包括 Api 30、Android 11 :

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

步骤:1

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

步骤: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 ;

}

答案 6 :(得分:0)

尝试添加

FPath.mkdirs(); (见http://developer.android.com/reference/java/io/File.html

然后只需将文件保存到该路径,Android OS将创建所需的所有目录。 您不需要执行存在检查,只需设置该标志并保存即可。 (另见:How to create directory automatically on SD card

答案 7 :(得分:0)

如果您尝试在存储中的应用目录内创建一个文件夹。

第1步:添加权限

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

第2步:添加以下内容

private String createFolder(Context context, String folderName) {

    //getting app directory
    final File externalFileDir = context.getExternalFilesDir(null);

    //creating new folder instance
    File createdDir = new File(externalFileDir.getAbsoluteFile(),folderName);

    if(!createdDir.exists()){

    //making new directory if it doesn't exist already
        createdDir.mkdir();

    }
    return finalDir.getAbsolutePath() + "/" +  System.currentTimeMillis() + ".txt";
}

答案 8 :(得分:-1)

尝试{

String filename =“ SampleFile.txt”;     字符串filepath =“ MyFileStorage”;

$claim->patientFirstName
$claim->patientMiddleName