8

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?

viedee
  • 1,647
  • 2
  • 10
  • 10
Jyosna
  • 4,436
  • 13
  • 60
  • 93
  • http://stackoverflow.com/questions/2895853/storing-data-on-sd-card-in-android http://stackoverflow.com/questions/5295041/saving-canvas-drawing-to-sd-card – Atul Goyal Aug 02 '11 at 18:06

2 Answers2

29

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

    }
Rasel
  • 15,499
  • 6
  • 40
  • 50
  • 1
    Thank you for ur answer. For addding image to that folder what i will do? – Jyosna Aug 02 '11 at 11:12
  • save the image to sd card from canvas – Jyosna Aug 02 '11 at 11:21
  • 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. – Edward Falk Dec 05 '12 at 08:38
  • 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 – Bagusflyer May 29 '13 at 01:19
7

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.

Ceetn
  • 2,728
  • 2
  • 25
  • 28
Naresh
  • 1,336
  • 9
  • 14