3

I have a Nexus 9 device. I would create a personal folder in the /sdcard/ path, something like this:

/sdcard/MyFolder/

so i coded this:

File directory = new File("/sdcard/MyFolder/");
    if(directory.mkdirs()){
        Toast.makeText(context, "Folder created", Toast.LENGTH_SHORT).show();
    }else{
        Toast.makeText(context, "Folder not created", Toast.LENGTH_SHORT).show();
    }

When I launch the app, it shows me "Folder not created" and so it doesn't create folder called MyFolder into /sdcard/ path. In Nexus 9 device the /storage/emulated/0path doesn't exist, so I have to use /sdcard/ path to accessing my storage.

I also used permission in my AndroidManifest.xml file, in this way:

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

Can you tell me please what's wrong? Thanks

dur
  • 15,689
  • 25
  • 79
  • 125
xXJohnRamboXx
  • 739
  • 3
  • 10
  • 24
  • In addition, since a Nexus 9 should be running Android 6.0+, if your `targetSdkVersion` is 23 or higher, you need to handle the permission at runtime: https://stackoverflow.com/questions/32635704/cant-get-the-permission – CommonsWare Apr 08 '16 at 11:58

2 Answers2

4

Since lots of devices have different file structures it is safer to use Environment.getExternalStorageDirectory() for determining the SD card path.

Can you try this code

File folder = new File(Environment.getExternalStorageDirectory() + "/MyFolder");
if (!folder.exists()) {
    folder.mkdir();
}
Niels Masdorp
  • 2,534
  • 2
  • 18
  • 31
0

try to specify external storage as listed below:

File directory = new File(Environment.getExternalStorageDirectory().toString()+"/MyFolder");
Matis11
  • 155
  • 2
  • 7