0

i need to create a folder kalid on android external removable storage device. i have included manifest read and write permissions.But my folder is created on phone storage. i a have implemented both codes from tutorials.

String storagestate = Environment.getExternalStorageState();

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

            String folder_main = "Kalids";
            File f = new File(Environment.getExternalStorageDirectory(), folder_main);


            if (!f.exists()) {
                f.mkdir();
            }
        };

The above code creates kalids folder in internal memory. also this code

String folder_main = "Kalid";

        File f = new File(Environment.getExternalStorageDirectory(), folder_main);
        if (!f.exists()) {
            f.mkdirs();
        }

my manifest look like below

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

how to create folder in external storage device?

  • Please post the manifest, because your method of saving is somewhat discouraged – Remario Jan 24 '18 at 19:34
  • @remario i have added it –  Jan 24 '18 at 19:36
  • Before i post an answer, the way you are doing it is wrong, FileProvider is the class you need, read up on it – Remario Jan 24 '18 at 19:40
  • Possible duplicate of [Android : FileProvider on custom external storage folder](https://stackoverflow.com/questions/37074872/android-fileprovider-on-custom-external-storage-folder) – Remario Jan 24 '18 at 19:41
  • `getExternalStorageDirectory(),`. As the function name tells you that is for external memory which is differend from removable storage like a micro sd card. – greenapps Jan 24 '18 at 20:45
  • `how to create folder in external storage device?`. So your question is wrong. – greenapps Jan 24 '18 at 20:48
  • I have read google documentation https://developer.android.com/training/data-storage/files.html on this site and it mentions that Externeal storage is "It's not always available, because the user can mount the external storage as USB storage and in some cases remove it from the device." also here is code to save file on external storage/* Checks if external storage is available for read and write */ public boolean isExternalStorageWritable() { String state = Environment.getExternalStorageState(); am confused about storage devices –  Jan 25 '18 at 04:49
  • @greenapps why my question is wrong? –  Jan 25 '18 at 04:54
  • Because you want to write on an sd card. On an removable medium. And your 'externalstorage' functions are for external storage. Not for removable. – greenapps Jan 25 '18 at 13:23

1 Answers1

0
boolean isAvialable= false;
        boolean isPermissable= false;
        String storagestate = Environment.getExternalStorageState();

        if (storagestate.equals(Environment.MEDIA_MOUNTED)) {
            isAvialable = true;
            isPermissable = true;

            String folder_main = "kalid";
            File f = new File(Environment.getExternalStorageDirectory(), folder_main);


            if (!f.exists()) {
                f.mkdir();
            }
        } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(storagestate)) {

            isAvialable = true;
            isPermissable = false;
        } else {

            isAvialable = isPermissable = false;
            Toast.makeText(MainActivity.this, "Read Only Media Can't Write" ,Toast.LENGTH_LONG).show();
        }

creates folder kalid in sdcard. Please don't forget to add runtime permission for marshmallow.