-1

i m trying to create directory in sd card through this card.but it is not worked.i put write external storage permission in manifest though it is not working.need urgent help.i had put this code in try catch.but it doesn't enter in catch block.here is my code..

try
        {
          File songDirectory = new File(Environment.getExternalStorageDirectory().toString()+"/iAC2013");
           if(!songDirectory.exists())
          {
              songDirectory.mkdirs();
              Toast.makeText(getApplicationContext(),"Directory created", Toast.LENGTH_LONG).show();
                // ShowlistView();
          }
         else
         {
               //ShowlistView();
               Toast.makeText(getApplicationContext(),"Directory AlreadyExists", Toast.LENGTH_LONG).show();
         }
        }
        catch(Exception e)
        {
            Log.e("","Error While creating file is:::::"+e+"");
        }
Praful Bhatnagar
  • 7,425
  • 2
  • 36
  • 44
Mikin Patel
  • 431
  • 2
  • 6
  • 15

3 Answers3

0

Try out the Below Code :

File sdDir = Environment.getExternalStorageDirectory();
            File wwwjdicDir = new File(sdDir.getAbsolutePath() + "/your_folder_name");
            if (!wwwjdicDir.exists()) {
                wwwjdicDir.mkdir();
            }

            if (!wwwjdicDir.canWrite()) {
                return;
            }
Bhavesh Patadiya
  • 25,740
  • 15
  • 81
  • 107
  • Bhavesh.....your code helped me. !wwwjdicDir.canWrite() executes while i runnige code. there was some problem while witing on sdcard.thanks. – Mikin Patel Mar 12 '13 at 05:11
  • @MikinPatel: you should accept the Answer is it helps you to solve your Problem. so that other User can rely on this answer to solve their issue. – Bhavesh Patadiya Mar 12 '13 at 07:59
0

try like

       File dir = new File(DEFAULT_STORAGE_LOCATION);
        // test dir for existence and writeability
        if (!dir.exists()) {
            try {
                dir.mkdirs();
            } catch (Exception e) {   
                return null;
            }
        } else {
            if (!dir.canWrite()) {
                   return null;
                 // do your work here
            }
        }
Ankitkumar Makwana
  • 3,475
  • 3
  • 19
  • 45
-1

If this is your code

File songDirectory = new `File(Environment.getExternalStorageDirectory().toString()+"/iAC2013");

you need to chagne it to this

File songDirectory = new File(Environment.getExternalStorageDirectory().toString()+"/iAC2013");
Rotary Heart
  • 1,899
  • 3
  • 29
  • 44