how to create a folder in android External Storage Directory?

巧了我就是萌 提交于 2019-12-17 15:33:18

问题


I cannot create a folder in android External Storage Directory.

I have added permissing on manifest,

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

Here is my code:

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

回答1:


Do it like this :

String folder_main = "NewFolder";

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

If you wanna create another folder into that :

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



回答2:


The difference between mkdir and mkdirs is that mkdir does not create nonexistent parent directory, while mkdirs does, so if Shidhin does not exist, mkdir will fail. Also, mkdir and mkdirs returns true only if the directory was created. If the directory already exists they return false




回答3:


I can create a folder in android External Storage Directory.

I have added permissing on manifest,

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

Here is my code:

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(); // This will create a Folder

  • inerDire.createNewFile(); // This will create File (For E.g .jpg
    file)




回答4:


we can Create Folder or Directory on External storage as :

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

and if we want to create Directory or folder on Internal Memory then we will do :

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

But make Sure you have given Write External Storage Permission. And Remember that if you have no external drive then it choose by default to internal parent directory. I'm Sure it will work .....enjoy code




回答5:


Try adding

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

and then just save the file as needed to that path, Android OS will create all the directories needed. You don't need to do the exists checks, just set that flag and save. (Also see : How to create directory automatically on SD card




回答6:


try {

String filename = "SampleFile.txt"; String filepath = "MyFileStorage";

                FileInputStream fis = new FileInputStream(myExternalFile);
                DataInputStream in = new DataInputStream(fis);
                BufferedReader br =
                        new BufferedReader(new InputStreamReader(in));
                String strLine;
                while ((strLine = br.readLine()) != null) {
                    myData = myData + strLine;
                }
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
          inputText.setText(myData);
            response.setText("SampleFile.txt data retrieved from External Storage...");
        }
    });

    if (!isExternalStorageAvailable() || isExternalStorageReadOnly()) {
        saveButton.setEnabled(false);
    }
    else {
        myExternalFile = new File(getExternalFilesDir(filepath), filename);
    }


来源:https://stackoverflow.com/questions/24781213/how-to-create-a-folder-in-android-external-storage-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!