I try to create a directory and use the following code:
boolean success = true;
String rootDirectory = Environment.getExternalStorageDirectory().toString();
folder = new File(rootDirectory, "Directory");
if(!(folder.exists())) {
success = folder.mkdirs();
} else {
}
if(success) {
Toast.makeText(getActivity().getApplicationContext(), "DIR created", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getActivity().getApplicationContext(), "DIR not created successfully", Toast.LENGTH_SHORT).show();
}
I also searched for the folder if it was created, there is none.
Permissions are granted:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
I also tried to ask for permission during runtime, it seems like the app has got the permission, therefore it cannot be the problem.
Some months ago I created another application and used identical code and the identical Sdk version, still it does not work with this one. I get "DIR not created successfully" and I do not know why, please help me figure out why I cannot create the directory.
Use code below
File directory = new File(Environment.getExternalStorageDirectory() + java.io.File.separator +"Directory");
if (!directory.exists())
Toast.makeText(getActivity(),
(directory.mkdirs() ? "Directory has been created" : "Directory not created"),
Toast.LENGTH_SHORT).show();
else
Toast.makeText(getActivity(), "Directory exists", Toast.LENGTH_SHORT).show();
For Android API 23 (Marshmallow) and greater, we have to allow dangerous permissions otherwise our code will not work as we expected.