0

I'm trying to create a directory in an SD card using the below code.

String state = Environment.getExternalStorageState();

    if (state.equals(Environment.MEDIA_MOUNTED)) {

        String secondaryStore = System.getenv("SECONDARY_STORAGE");
        File videoDirectory = new File(secondaryStore + File.separator +"video_files");

        if(!videoDirectory.exists()){
            boolean sd_mkdirs = videoDirectory.mkdirs();
            if(sd_mkdirs){
                Log.d(TAG,"file Created");
            }
        }

    }else{

        File videoDirectory = new File(Environment.getExternalStorageDirectory() + File.separator + "video_files");

        if(!videoDirectory.exists()){
            boolean internal_mkdirs = videoDirectory.mkdirs();
            if(internal_mkdirs){
                Log.d(TAG,"file Created");
            }
        }
    }

This code checks whether or not the SD card is mounted. If true It creates a directory in the SD card, otherwise in internal storage. However, when it is suppossed to make a directory on the SD card _mkdirs returns false. What am I doing wrong ?

This is my AndroidManifest file. I think I included the permission to write.

<?xml version="1.0" encoding="utf-8"?>

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

<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • 1
    You are confusing [internal storage](https://commonsware.com/blog/2014/04/07/storage-situation-internal-storage.html), [external storage](https://commonsware.com/blog/2014/04/08/storage-situation-external-storage.html), and [removable storage](https://commonsware.com/blog/2014/04/09/storage-situation-removable-storage.html). `getExternalStorageState()` has nothing to do with removable storage, and there is no requirement for a device to have a `SECONDARY_STORAGE` environment variable. – CommonsWare Oct 25 '16 at 18:10
  • this question is running and can be relevant for you http://stackoverflow.com/questions/40068984/universal-way-to-write-to-external-sd-card-on-android – Maytham Fahmi Oct 25 '16 at 20:26
  • `if(sd_mkdirs){ Log.d(TAG,"file Created");`. You mean 'directory created'? And why not an else if it fails? – greenapps Oct 25 '16 at 22:18
  • `System.getenv("SECONDARY_STORAGE");` is unreliable and returns `null` on many systems. Check for null before use. – greenapps Oct 25 '16 at 22:20
  • Which Android version is in play? – greenapps Oct 25 '16 at 22:20
  • You have two mkdirs. But you are not telling which one fails. – greenapps Oct 25 '16 at 22:25
  • Have you ever seen that state is not MEDIA_MOUNTED? – greenapps Oct 25 '16 at 22:25

0 Answers0