0

I am trying to create a folder and then after that do some file IO operations! I am using a sony Xperia Z to test this out! I know right now I've hardcoded the location but it doesn't let me create folders!

  File appPath = new File("/storage/sdcard1/folder");
        if (!appPath.exists()) {
            appPath.mkdirs();
}

I am using a targetSdkVersion of 22 And having lollipop on my phone. I tried

appPath.mkDir();

as well but all this gives a value of False. And i have added permissions to manifest

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

And I tried many different open source file manager but none are able to create folders, But ES file Manager is able to create folders and do File IO operations!

Kunal
  • 953
  • 1
  • 10
  • 14

1 Answers1

0

Don't hardcode global paths like /sdcard, use Environment.getExternalStorageDirectory() and related methods instead. Here is a working sample.

   public static String getNewFolderPath() {
        File folder = new File(Environment.getExternalStorageDirectory()
                .toString() + File.separator + "folder");
        if (!folder.exists())
            folder.mkdirs();
        return folder.getAbsolutePath();
    }

EDIT: For more info check:

Hope it helps!

Community
  • 1
  • 1
Gueorgui Obregon
  • 5,077
  • 3
  • 33
  • 57
  • getExternalStorageDirectory gives /storage/emulated/0 which is the internal storage in my phone! Btw the problem is not about hardcoding, the problem is about i cannot write in SD Card! – Kunal Feb 28 '16 at 07:59
  • Yes I understand, but `Environment.getExternalStorageDirectory()` is for getting SD Card path, I edited my answer with some links that will explain you, your actual problem. Good luck :) – Gueorgui Obregon Feb 28 '16 at 08:11
  • 1
    No. getExternalStorageDirectory() is for getting path to external storage. And in most devices that is not the path to a removable micro sd card. – greenapps Feb 28 '16 at 10:29
  • @g2o I don't have the problem with getting the SD card path or internal storage path, the point is I'm not able to write to an external SD card even if i hardcode the correct path! – Kunal Feb 28 '16 at 10:40