how to create a folder in android External Storage Directory?

后端 未结 8 1133
耶瑟儿~
耶瑟儿~ 2020-12-01 04:39

I cannot create a folder in android External Storage Directory.

I have added permissing on manifest,



        
相关标签:
8条回答
  • 2020-12-01 04:50

    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

    0 讨论(0)
  • 2020-12-01 04:50

    If you are trying to create a folder inside your app directory in your storage.

    Step 1 : Add Permission

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

    Step 2 : Add the following

    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";
    }
    
    0 讨论(0)
  • 2020-12-01 04:53

    The use of Environment.getExternalStorageDirectory() now is deprecated since API level 29, the option is using:

    Context.getExternalFilesDir().

    Example:

    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();
    }
    
    0 讨论(0)
  • 2020-12-01 04:58

    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

    0 讨论(0)
  • 2020-12-01 05:04

    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);
        }
    
    0 讨论(0)
  • 2020-12-01 05:08

    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();
    }
    
    0 讨论(0)
提交回复
热议问题