0

I am trying to create a directory in android for storing images taken by the camera app.

I have included the permission in the manifest file

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

I have also followed the answers given here :

Android mkdirs() sdcard do not work

File mkdirs() method not working in android/java

Android mkdirs() not working

Android mkdir not making folder

Android failed to mkdir() on SD card

and a few others and all contain similar answers

This is what I have written

   if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
        Log.i(TAG, "Storage Location read write permission available");
        String root = Environment.getExternalStorageDirectory().toString();
        File imagesFolder = new File(root + "/ImgDir");
        if(imagesFolder.mkdirs()){
            Log.i(TAG, "Storage Location is ready");
        }else{
            Log.i(TAG, "Storage Location is not ready");
        }
    }
    else{
        Log.i(TAG, "Storage Location read write permission not available");
    }

The logcat shows "Storage Location is not ready". Can anyone tell me why the directory is not being created.

If the directory already exists then the application saves the image taken by the camera to the directory, but if it does not exist then the directory is not being created.

I am new to android, is there anything else that I am missing?

I am also getting false for all 4 of these lines :

Environment.getExternalStorageDirectory().canRead()
Environment.getExternalStorageDirectory().canWrite()
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).canRead()
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).canWrite()
Community
  • 1
  • 1
Himanshu Tanwar
  • 906
  • 6
  • 18
  • Replace `new File(root + "/ImgDir")` with `new File(Environment.getExternalStorageDirectory(), "ImgDir")` and see if you have better luck. Never use concatenation to create a `File` object -- let the `File` constructor do that, so it properly handles adding or removing `/` characters as needed. Also, please consider not creating your own directory here, but rather under `Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)`, as users get testy when you clutter up the root of their external storage. – CommonsWare Jun 07 '15 at 11:41
  • @CommonsWare it didnt't work. I also cleaned up my project and restarted Android Studio – Himanshu Tanwar Jun 07 '15 at 12:01
  • Did you try to change this line: String root = Environment.getExternalStorageDirectory().toString(); to String root = Environment.getExternalStorageDirectory().getPath(); ? – blueware Jun 07 '15 at 12:23
  • @blueware yes I tried that, still didn't work. – Himanshu Tanwar Jun 07 '15 at 12:30
  • Did you try if(imagesFolder.exists() || imagesFolder.isDirectory()) instead of if(imagesFolder.mkdirs()) as well? Also, use: if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) instead of your check, because .equals method returns true if both objects are the same – blueware Jun 07 '15 at 12:38

0 Answers0