Android create folders in Internal Memory

I used this to create folder/file in internal memory :

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

Android change his security about Storage, For more details check the video Storage access with Android 10

Also you can try this in android 10

File mydir = new File(getApplicationContext().getExternalFilesDir("Directory Name").getAbsolutePath());
    if (!mydir.exists())
    {
        mydir.mkdirs();
        Toast.makeText(getApplicationContext(),"Directory Created",Toast.LENGTH_LONG).show();
    }

The path of your directory will be in your app data.


YOU SHOULD NOT USE THIS IF YOU WANT YOUR FILES TO BE ACCESSED BY USER EASILY

File newdir= context.getDir("DirName", Context.MODE_PRIVATE);  //Don't do
if (!newdir.exists())
    newdir.mkdirs();

INSTEAD, do this:

To create directory on phone primary storage memory (generally internal memory) you should use following code. Please note that ExternalStorage in Environment.getExternalStorageDirectory() does not necessarily refers to sdcard, it returns phone primary storage memory

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

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

Directory created using this code will be visible to phone user easily. The other method (mentioned first) creates directory in location (/data/data/package.name/app_MyDirName), hence normal phone user will not be able to access it easily and so you should not use it to store video/photo etc.

You will need permissions, in AndroidManifest.xml

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

context.getDir("mydir", ...); This creates your.package/app_mydir/

 /** Retrieve or creates <b>path</b>structure inside in your /data/data/you.app.package/
 * @param path "dir1/dir2/dir3"
 * @return
 */
private File getChildrenFolder(String path) {
            File dir = context.getFilesDir();
    List<String> dirs = new ArrayList<String>(Arrays.<String>asList(path.split("/")));
    for(int i = 0; i < dirs.size(); ++i) {
        dir = new File(dir, dirs.get(i)); //Getting a file within the dir.
        if(!dir.exists()) {
            dir.mkdir();
        }
    }
    return dir;
}