Gopi
Gopi

Reputation: 1

Simple creation of folder under SDCARD in Android Studio

I've used the code to create a folder under SD card. But the folder never creates.I've added uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" to manifest.

 File folder = new File(Environment.getExternalStorageDirectory().toString()+"/Images");
       folder.mkdirs();


       if(folder.exists()){
           Toast.makeText(getApplicationContext(),"exists",Toast.LENGTH_LONG).show();
       }else{

           Toast.makeText(getApplicationContext()," not exists",Toast.LENGTH_LONG).show();
       }

Upvotes: 0

Views: 3658

Answers (5)

Gopi
Gopi

Reputation: 1

Problem solved ! I have added the block in the wrong section of the manifest. It should precede or follow the section.

Upvotes: 0

lcw_gg
lcw_gg

Reputation: 669

You should also add:

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

Upvotes: 0

Somesh Kumar
Somesh Kumar

Reputation: 8638

Try this and check the flow of Log-->

      //First check if the sd card is mounted or not      
         if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))

                {
                   // sd card mounted
                   Log.d("SDCard", "SDcard is present");
                }  

                File folder = new File(Environment.getExternalStorageDirectory() + File.separator +"Images/");
             if(!folder.exists())

              {
                folder.mkdirs();
                Log.d("SDcard", "Folder created");
              }   
            else
         {
                Log.d("SDCard", "Folder already exists");
         }       
     }

Upvotes: 0

EdoB
EdoB

Reputation: 91

Try to add this code to debug whether you can actually write on the external storage or not:

Log.d("MyActivity", "Can write: " + Environment.getExternalStorageDirectory().canWrite());

and verify the result of mkdirs too:

boolean result = folder.mkdirs();
Log.d("MyActivity", "mkdirs: " + result);

Upvotes: 1

Mathankumar K
Mathankumar K

Reputation: 117

File directory = new File(pathname);
                if (directory != null && !directory.exists()
                        && !directory.mkdirs()) {
                    try {
                        throw new IOException("Cannot create dir "
                                + directory.getAbsolutePath());
                    } catch (IOException e) {

                        e.printStackTrace();
                    }
                }

Upvotes: 0

Related Questions