In this article we will check if certain folder present in our local directory and if specific folder is not present the how to create new folder programmatically.
Create new folder if not present
public static void checkIfDirectoryExist(String folderName) {
String dirFullPath = Environment.getExternalStorageDirectory().toString() + "/"+folderName;
File getPath = new File(dirFullPath);
// Check is specific directory exist on local env
if(getPath.exists()){
boolean mkdir = getPath.mkdir();
} else {
System.out.println("Dir already present");
}
}
Here on line number 3 we are using Environment.getExternalStorageDirectory() method to get initial path of our local storage. Which will be /storage/emulated/0
Make sure you add “/” before folder name.so the complete path will be like /storage/emulated/0/Folder Name
Calling method in onCreate()
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkIfDirectoryExist("Folder Name");
}
This is how we create new folder in android using android studio and java , If you have any question or suggestion then please put it in comment below.
Thank you.