Make directory in android

后端 未结 8 689
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-13 17:40

Im trying to build a directory called \"images\" on the SD card on android. This is my code but its not working? Can anyone give me some advice?

File picDire         


        
8条回答
  •  情话喂你
    2020-12-13 18:14

    To create file inside sd card you have to use Environment.getExternalStorageDirectory()

       /**
         * Creates a new directory inside external storage if not already exist.
         *
         * @param name The directory name
         */
        public static void createNewDirectory(String name) {
                // create a directory before creating a new file inside it.
                File directory = new File(Environment.getExternalStorageDirectory(), name);
                if (!directory.exists()) {
                    directory.mkdirs(); 
                }
            }
    

    Following two important parameter which helps you to create directory 1. directory.mkdirs() :

    Creates the directory named by this file, creating missing parent directories if necessary. 2. directory.mkdir() :

    Creates the directory named by this file, assuming its parents exist.

    For more you can how getExternalStorageDirectory() works please see link

提交回复
热议问题