-1

Android Manifest Permissions

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

Java file (First try)

String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/newAppFolder";
File dir = new File(path);
dir.mkdirs();

Java file (Second try)

String state = Environment.getExternalStorageState();
if(Environment.MEDIA_MOUNTED.equals(state)) {
    File dir = new File(Environment.getExternalStorageDirectory(), "newAppFolder");
    if(!dir.exists()) {
        dir.mkdirs();
        Toast.makeText(getApplicationContext(), "Folder Created", Toast.LENGTH_LONG).show();
    }
    else
        Toast.makeText(getApplicationContext(), "Folder exists", Toast.LENGTH_LONG).show();
    }

else
    Toast.makeText(getApplicationContext(), "SD Card Not Found", Toast.LENGTH_LONG).show();

Unfortunately neither attempt creates the newAppFolder file that I need. Can someone please tell me what I'm doing wrong?

Note: In the second try, the application toast keeps showing Folder Created

Floern
  • 33,559
  • 24
  • 104
  • 119
Ravin
  • 13
  • 1
  • 5
  • 2
    If your target sdk is 23, you should check [this link](https://developer.android.com/training/permissions/requesting.html) and see if requesting permission at runtime fixes it. – Vucko Jun 29 '16 at 19:18
  • @Vucko I don't think that could be the issue as "Folder Created" is displayed. – Shadab Ansari Jun 29 '16 at 19:37
  • @ShadabAnsari can you see that that toast will be displayed if the folder does not exist, and if `mkdirs()` does not throw an exception? It will display the toast even if the folder's not actually created. – Vucko Jun 29 '16 at 19:41
  • @Vucko That's what I meant, lack of permissions would have lead to the exception and Toast would not have been visible. – Shadab Ansari Jun 29 '16 at 19:47
  • @ShadabAnsari you're quite right. I have no idea what other thing it may be then.. – Vucko Jun 29 '16 at 19:47
  • @Vucko Thank you very much. That was the problem. It all works fine now – Ravin Jun 29 '16 at 19:59
  • No problem at all bro. I'll move my comment to the answer then, and you can accept it so it may help the others if a day comes and someone else stumbles upon the same problem. Apparently, it does not throw the Exception, just silently failes @ShadabAnsari. – Vucko Jun 29 '16 at 20:00

1 Answers1

0

Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app.

If your target sdk is 23, you should check this link and see if requesting permission at runtime fixes it.

Vucko
  • 7,371
  • 2
  • 27
  • 45