0

I need to create a folder in internal memory root directory and create a file inside it, but cannot find the below code working.

        String path = Environment.getRootDirectory().toString();

        File mFolder = new File(path,"Folder");
        if (!mFolder.exists()) {
            boolean res = mFolder.mkdir();
        }

And mkdir always return false. I already found getDataDirectory() and getFilesDir() but that I doesn't required. I need to create a directory where the internal memory root location(location we see first when we open internal memory)

Edit:

Root folder I mean the first location we see on internal memory open using file browser. Where I can see Download ,Pictures ,Android etc..

CodeDezk
  • 1,230
  • 1
  • 12
  • 40

4 Answers4

4

You should use getExternalStorageDirectory() and you should ask for write permissions to it.

But note getExternalStorageDirectory() was deprecated on android 29, that means you should use getExternalFilesDir(), getExternalCacheDir(), or getExternalMediaDir() instead if you target a newer android version depending on the contents of your files.

And you should ask for write permissions on the manifest (for old android versions, Build.VERSION.SDK_INT < 23) and ask for them on run time (for newer android versions, Build.VERSION.SDK_INT >= 23)

To check if the user has granted permission of external storage:

if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
    Log.v(TAG,"Permission granted");
    //File write logic here
    return true;
}

If the permission is not granted you should ask for it:

ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE);

and implement OnRequestPermissionResult to get the result callback.

All this info and more code can be found here https://developer.android.com/training/permissions/requesting

jeprubio
  • 17,312
  • 5
  • 45
  • 56
  • 1
    Environment.getExternalStorageDirectory() does it create folder in internal memory. – CodeDezk Feb 06 '20 at 08:22
  • getFilesDir() was not creating folder in root directory, please see my question. – CodeDezk Feb 06 '20 at 08:26
  • but you only can create files in your app's folder unless it's jailbroken – jeprubio Feb 06 '20 at 08:28
  • 1
    Yes I need to see the file using file browser, like whatsup create directory in internal memory and we can browse it. – CodeDezk Feb 06 '20 at 08:29
  • Root I mean when we open internal memory we can see the folder at first location. – CodeDezk Feb 06 '20 at 08:31
  • 1
    `You should use context.getFilesDir() and you should ask for write permissions to it.`. No. No permissions needed for getFilesDir(). – blackapps Feb 06 '20 at 08:32
  • But the getFilesDir() was not creating folder inside first directory of internal memory, I have tried it, it was creating inside /data/ directory. – CodeDezk Feb 06 '20 at 08:35
  • blackapps is right, every time I'm more shure CodeDezk is asking for `getExternalStorageDirectory()` which most file browsers show you as internal memory as it is not removable. The whatsapp media files and so on go to this directories. I guess that's the confusion – jeprubio Feb 06 '20 at 08:35
  • well, I've just checked and `getExternalStorageDirectory` is deprecated since API level 29. You should use `getExternalFilesDir()`, `getExternalCacheDir()`, or `getExternalMediaDir()` instead depending on the file contents in the case you target a newer android version (newer than 29). – jeprubio Feb 06 '20 at 08:49
  • @jeprubio this all method retrieve path of Android->Data->com.app.yourApp path not internal storage path.I am also finding for android Q which method gives me original path of internal storage not of data path because using this method when app will uninstall it's clear all data. – Hardik Talaviya Feb 06 '20 at 10:49
  • Which android version do you target? If it's 29 or newer you should use one of that three methods depending on the content of your files, if it's lower just use getExternalStorageDirectory. You can only target one android version in that module. Check at the gradle file (targetSdkVersion) – jeprubio Feb 06 '20 at 11:04
  • @jeprubio this three method are not give internal storage path It's gives Android->data path. – Hardik Talaviya Feb 06 '20 at 11:20
  • Ups, then there must be another way, After checking that it's deprecated on the oficial docs I got this three from here https://stackoverflow.com/questions/57116335/environment-getexternalstoragedirectory-deprecated-in-api-level-29-java But if you don't target android 29 or newer you are fine with getExternalStorageDirectory for the moment. Maybe ContextCompat.getExternalFilesDirs() is the answer – jeprubio Feb 06 '20 at 11:51
  • Did anyone find a way to create a folder at root level internal storage? I read the docs, it says if you want to retain data after app uninstall then don't keep such data in app directory. For targetSdk API 29 devices or above we can retain data by putting a tag in manifest. Please anyone let me know. @CodeDezk – User9211 Mar 13 '21 at 03:01
1

Try below code

File file = new File(Environment.getExternalStorageDirectory() + "/Folder");
if (!file.exists()) {
    boolean res = file.mkdirs();
}

But Environment.getExternalStorageDirectory() It's deprecated for Android Q.

I hope this can help you!

Thank You.

Hardik Talaviya
  • 1,396
  • 5
  • 18
1

I think you can't create a directory inside the internal storage of the device. Except you've a root access for the app. You can only create the directory inside your app private folder within the following path String path = getFilesDir().

you can use like this below -

File mydir = context.getDir("mydirectory", Context.MODE_PRIVATE); //Creating an internal dir;
File fileWithinMyDir = new File(mydir, "myAwesomeFile"); //Getting a file within the dir.
FileOutputStream out = new FileOutputStream(fileWithinMyDir); //Use the stream as usual to write into the file.

getDir(StringName, int mode) method to create or access directories in internal storage.

For more information you can read about this - create directory

Dipankar Baghel
  • 1,932
  • 2
  • 12
  • 24
0

mkdir()creates only the demanded directory and will return false if some of the parent directories doesn't exist. Try checking if the directories exist(or why not) or use mkdirs() which additionally creates the missing directiories

qki
  • 1,769
  • 11
  • 25