How to check if newly created folder is present into SD Card in Android

16,424

Solution 1

below code will create a directory if it does not exist

   File direct = new File(Environment.getExternalStorageDirectory() + "/New Folder");

   if(!direct.exists())
    {
        if(direct.mkdir()) 
          {
           //directory is created;
          }

    }

Solution 2

You should request the following permission first in your Android manifest :

android.permission.WRITE_EXTERNAL_STORAGE

and execute above code by Rasel for it to work.

Share:
16,424

Related videos on Youtube

Jyosna
Author by

Jyosna

Works at Amadeus Labs

Updated on June 04, 2022

Comments

  • Jyosna
    Jyosna almost 2 years

    From my application, I want to store some images into my SD card. For that I need to create a one folder.

    At the first time folder will create but after it checks whether that folder is present or not. How can I do it?

  • Jyosna
    Jyosna almost 13 years
    Thank you for ur answer. For addding image to that folder what i will do?
  • Jyosna
    Jyosna almost 13 years
    save the image to sd card from canvas
  • Edward Falk
    Edward Falk over 11 years
    Couple of notes: you should be checking getExternalStorageDirectory() for a null return and/or calling getExternalStorageState() first. To save a file in your new directory, do "File ofile = new File(direct, ofilename);" and "FileWriter fw = new FileWriter(ofile);". Then write your image data to fw. Don't forget to close it when you're done. Wrapping it in a BufferedWriter might improve performance, depending on your application.
  • Bagusflyer
    Bagusflyer almost 11 years
    What if I want to create multiple level folder. For example /New Folder1/New Folder2 ? Is there a simple call like ios sdk does? Thanks