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

时间:2016-03-21 07:15:19

标签: android android-external-storage

这就是我的尝试。

 private void createFolderInExternalStorage() {
    String storagePath = System.getenv("SECONDARY_STORAGE");
    Log.e("storagePath->",storagePath);
    String path = "not available";
    if (storagePath != null) {
        Log.e("Path->", "" + storagePath);
        File file = new File(storagePath.toString());
        Log.e("readable->", "" + file.canRead());
        Log.e("writable->", "" + file.canWrite());
        Log.e("executable->", "" + file.canExecute());
        dir = new File(storagePath + File.separator+etFolder.getText().toString());
        if (!dir.exists()) {
            dir.mkdirs();
            Toast.makeText(this,"Folder "+etFolder.getText().toString()+" created",Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(this,"Folder "+etFolder.getText().toString()+" already exists",Toast.LENGTH_SHORT).show();
        }
        path = dir.getPath();
    } else {
        Toast.makeText(this,"External Storage not available",Toast.LENGTH_SHORT).show();
    }
    tv.setText("External SDCARD path->" + path);
}
  

如果存在辅助存储,那么System.getenv(“SECONDARY_STORAGE”)在我的情况下返回/ storage / sdcard1但是得到以下内容:

     

03-21 12:02:26.827 14155-14155 / com.think.teststorage E / readable->:false

     

03-21 12:02:26.827 14155-14155 / com.think.teststorage E / writable->:false

     

03-21 12:02:26.828 14155-14155 / com.think.teststorage E / executable->:false

     

即使在某些设备中,上述状态为true,但文件夹创建失败。

我已添加权限:

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

欢迎提出建议。

4 个答案:

答案 0 :(得分:2)

您可以使用此代码创建文件夹。

File dir = new File("path/of/your/folder");
try{
  if(dir.mkdir()) {
     System.out.println("Folder created");
  } else {
     System.out.println("Folder is not created");
  }
}catch(Exception e){
  e.printStackTrace();
}

另外添加此权限:

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

供进一步参考:see this link

如果这适合您,请告诉我! :)

答案 1 :(得分:1)

使用以下行:

        //Define the path you want
        String path = Environment.getExternalStoragePublicDirectory(Environment.YOUR_DIRECTORY) + File.separator + "YourFolderName";

        File file = new File(path);
        if (!file.exists()) {
            file.mkdirs();
        }

YOUR_DIRECTORY是您要创建文件夹的目录,例如:DIRECTORY_DOCUMENTS,DIRECTORY_DOWNLOADS,DIRECTORY_PICTURES等。

在你的清单中应该添加写入权限:

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

希望有所帮助!

答案 2 :(得分:0)

在android

中非常简单明了
`
    File mFolder = new File(Environment.getExternalStorageDirectory(), "Folder_Name");
    if (!mFolder.exists()) {
        mFolder.mkdirs();
        mFolder.setExecutable(true);
        mFolder.setReadable(true);
        mFolder.setWritable(true);
    }
`

还包括清单中所需的权限 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

答案 3 :(得分:0)

只需输入以下代码行,

// create a File object for the parent directory
File wallpaperDirectory = new File("/sdcard/Wallpaper/");
// have the object build the directory structure, if needed.
wallpaperDirectory.mkdirs();
// create a File object for the output file
File outputFile = new File(wallpaperDirectory, filename);
// now attach the OutputStream to the file object, instead of a String representation
FileOutputStream fos = new FileOutputStream(outputFile);

并添加require权限

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

不适用于KitKat,外部物理SD卡 然后使用

  

Environment.getExternalStorageDirectory()的getPath();