Reputation: 685
This is my directory structure on my Android sdcard
sdcard/alQuranData/Reader1/Surah
Here is my code to make Directories
File SDCardRoot = new File(Environment.getExternalStorageDirectory().toString() + "alQuranData/Reader1/Surah");
Toast.makeText(getApplicationContext(), SDCardRoot.toString(), Toast.LENGTH_LONG).show();
if (!SDCardRoot.exists()) {
Log.d("DIRECTORY CHECK", "Directory doesnt exist creating directory");
SDCardRoot.mkdir();
}
Now alQuranData
is already created in my sdcard
root. If i only create Reader1 directory than it works fine, but when is add Reader1/Surah
than it did not create.
I also tried mkdirs()
but it doesn't work.
Upvotes: 2
Views: 2968
Reputation: 7435
Are you getting any error or exception. Please try to check the return value of mkdirs()
method call. ALso try the following code:
File SDCardRoot = new File(Environment.getExternalStorageDirectory().toString() + "/alQuranData/Reader1/Surah");
Toast.makeText(getApplicationContext(), SDCardRoot.toString(), Toast.LENGTH_LONG).show();
if (!SDCardRoot.exists()) {
Log.d("DIRECTORY CHECK", "Directory doesnt exist creating directory");
SDCardRoot.mkdirs();
}
Please also check that you have added following permission in manifest file:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
I just tested the following code and it is working on my end:
File SDCardRoot = new File(Environment.getExternalStorageDirectory()
.toString() + "/alQuranData/Reader1/Surah");
Toast.makeText(getApplicationContext(), SDCardRoot.toString(),
Toast.LENGTH_LONG).show();
if (!SDCardRoot.exists()) {
Log.d("DIRECTORY CHECK",
"Directory doesnt exist creating directory "
+ Environment.getExternalStorageDirectory()
.toString());
boolean outcome = SDCardRoot.mkdirs();
Log.d("DIRECTORY CHECK",
"outcome for " + SDCardRoot.getAbsolutePath() + " "
+ outcome);
}
I have added alQaranData folder manually as mentioned in your post and added the permission and it start working at my end. Please check this code.
Upvotes: 3
Reputation: 11
By default there is no way in android to create multiple directory at once. you have to create one by one and check is created or not.
But I have created a below code to achieve similar objective. you can use to create multiple directory at once...
public static boolean recursivelyMakeDir(File endDirectory,File baseDir) {
if (!baseDir.exists()){
return false;
}
ArrayList<String> allDirectoryList = parseAndGetListDir(endDirectory.getAbsolutePath().substring(baseDir.getAbsolutePath().length()));
File currentDir = baseDir;
for (int i=0; i< allDirectoryList.size() ; i++){
File dir = new File(currentDir.getAbsolutePath() + "/" + allDirectoryList.get(i) );
if (!dir.exists()){
if (!dir.mkdir()){
return false;
}
}
currentDir = dir;
}
return true;
}
public static ArrayList<String> parseAndGetListDir(String data) {
ArrayList<String> allDirectoryList = new ArrayList<>();
boolean exit = false;
String dir = "";
for (int i=0; i<data.length() ; i++){
if (data.charAt(i) == '/'){
if (!dir.equals("")){
allDirectoryList.add(dir);
dir = "";
}
}else {
dir = dir + data.charAt(i);
}
}
if (!dir.equals("")){
allDirectoryList.add(dir);
}
return allDirectoryList;
}
Usage :
// you 100% sure that this directory is already exist.
File baseDir = Environment.getExternalStorageDirectory();
// desire directory wanted to create.
File endDirectory = new File(Environment.getExternalStorageDirectory() +"kkk/SSS/aaa/ddd/b");
if(recursivelyMakeDir(endDirectory ,baseDir )){
// dir created sucessessfully
}else{
// failed
}
Upvotes: -1
Reputation: 1056
Use this code.....)
private void createSdCatalogs(String str1){
//str1 = "/alQuranData"
File folder = new File(Environment.getExternalStorageDirectory() + str1);
boolean success = false;
if (!folder.exists()) {
success = folder.mkdir();
Log.v("Adding line", "/");
}}
private void createSdCatalogs(String str2) {
//str2 = "/alQuranData/Reader1";
File folder1 = new File(Environment.getExternalStorageDirectory() + str2);
boolean success1 = false;
if (!folder1.exists()) {
success1 = folder1.mkdir();
Log.v("Adding line", "/");
}
}
Upvotes: 0
Reputation: 6731
Using Java you cannot create multiple directories all at once. You will need to declare a folder name one by one and proceed to create a directory. So first create
Using the same piece of code you have provided.
Upvotes: 0