1

I'm trying to generate a folder with my android application in my phone storage (not on the sdcard) but my mkdirs() is not working.

I have set the android.permission.WRITE_EXTERNAL_STORAGE in my manifest and use this basic code :

    File mediaStorageDir = new File(Environment.getExternalStorageDirectory(), "/MyDirName");

    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.d("App", "failed to create directory");
        }
    }

but it doesn't work ... The mkdirs is always at false and the folder is not created.

I have tried everything and looked at all the topics about it but nothing is working and I don't know why.

Avihoo Mamka
  • 4,656
  • 3
  • 31
  • 44
N. Remon
  • 11
  • 1
  • 2
  • You need runtime permissions on Android API 23+ – OneCricketeer Apr 25 '17 at 13:08
  • From android 6.0, you need request permission. Add these lines before creating new file `ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE},1);` See more at [https://developer.android.com/training/permissions/requesting.html](https://developer.android.com/training/permissions/requesting.html) – Phạm Lam Apr 25 '17 at 13:11
  • "the folder is not created" -- how are you testing this? – CommonsWare Apr 25 '17 at 13:37
  • Thanks a lot for your answers ! I suppose my application works now but ... I don't see my folder. When I display my `mediaStorageDir` variable that's what I have : **/storage/emulated/0/MyDirName**. I don't found this folder, why ? – N. Remon Apr 25 '17 at 15:04
  • Now I have the folder in my android file explorer. It appears with no interaction with the app ! Aftet trying my app I've killed the file explorer to refresh it but my folder was not there. It just appeard now as if by magic ! Do you have an explanation ? – N. Remon Apr 25 '17 at 15:24
  • And I can't see this folder in my Windows file explorer !!!! Have you an explanation ?? – N. Remon Apr 25 '17 at 15:28

3 Answers3

3

if you target and compile sdk is higher then lolipop then please refer this link

or

File sourcePath = Environment.getExternalStorageDirectory();

                File path = new File(sourcePath + "/" + Constants.DIR_NAME + "/");

                path.mkdir();
Bhupat Bheda
  • 1,968
  • 1
  • 8
  • 13
  • Thanks ! For your second solution, my Android Studio don't recognize the `Constants.DIR_NAME` variable. Do I have to import something ? – N. Remon Apr 25 '17 at 15:09
0

If you you the emulator and the Device File Explorer of Android Studio, be sure that you right-click over a folder of the emulator and then click on 'synchronize' to update the files displayed. The Device File Explorer doesn't update by itself in real time.

toto_tata
  • 14,526
  • 27
  • 108
  • 198
0

when writing code for android API 29 and above use the following permission in your application manifest (AndroidManifest.xml)

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

Then in your java file add the following lines of code

    `ActivityCompat.requestPermissions(this, new String[]
                    {
                            Manifest.permission.READ_EXTERNAL_STORAGE,
                            Manifest.permission.WRITE_EXTERNAL_STORAGE
                    },
            PackageManager.PERMISSION_GRANTED);

    StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
    StrictMode.setVmPolicy(builder.build());

    file = new File(Environment.getExternalStorageDirectory().getPath(), "MyDirName/");
    if (!file.exists()) {
        try {
            file.mkdirs();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

`