0

I have been trying to fix an issue since yesterday but no luck yet. I made a very simple android application to create directory and the application was working fine. The main source code is mentioned here.

@Override public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main); 
    //View vi = null;

    File extDir= Environment.getExternalStorageDirectory(); 
    File sddir = new File(extDir+"/test10");  


    if (sddir.mkdirs()) {  
        Toast toast = Toast.makeText(this,  
        "Directory successfully created!",  
               Toast.LENGTH_SHORT);  
    toast.setGravity(Gravity.CENTER, 0, 0);  
    toast.show();  
    }
    else{  
       Log.e(TAG, "Create dir in sdcard failed");  
       Toast toast = Toast.makeText(this,  
       "Directory creation failed!",  
              Toast.LENGTH_SHORT);  
       toast.setGravity(Gravity.CENTER, 0, 0);  
    toast.show();  
   } 
   ..... followed by remaining code 

However, yesterday, when I integrated this code to my own application (a simple videolist that plays videos from the sd-card), the directory function, for whatever reasons, resulted in directory creation failed... I debugged the application but couldn't find exception errors or other errors in it. I don't know what could be wrong.. I am just wondering if there is any method to get the error statement behind directory creation failed. I mean if mkdirs failed, it could generate a small print statement about why it got failed?? any suggestions??

  • have you declare write external permission in manifest file ? – Chirag Jul 04 '11 at 10:38
  • yes, I have declared write external permission in the manifest file. The same code works when it is type of a stand alone code with no other functionality. however, i am using it with other code for listview etc etc but I don't think it should affect the working of directory creation. I have really no clue what is going wrong and I have spent hours and hours without getting any clue.. – Farhan Khurshid Jul 04 '11 at 10:44

2 Answers2

0

uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"

Rasel
  • 15,499
  • 6
  • 40
  • 50
0

Please try with below function.

File cacheDir = new File(android.os.Environment.getExternalStorageDirectory(),"test10");
if (!cacheDir.exists())
    cacheDir.mkdirs();

and declare <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> in manifest file.

Chirag
  • 56,621
  • 29
  • 151
  • 198
  • Thankyou Sir for your reply. Though the problem was not with permission or my code.. But your reply forced me to again go to my manifest file where I discovered that I had previously used permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" I removed it and now it is working fine.. Wish I could remove that before so I wouldn't have to waste 10 hours.. Thank you so much for your reply sir.. – Farhan Khurshid Jul 04 '11 at 10:58