matejko219 matejko219 - 1 month ago 13
Java Question

mkdir() created direcotry is seen as file

I have a problem. I work on project and I decide to change names of my app directories for users data. I change it in a code and delete it from phone to check if the app create new ones with right names. And then troubles begin. Directories are created but when I try to open directory in windows explorer it is seen as a file.

mkdir()
return true for each direcotry and when I check
Environment.getExternalStorageDirectory().canWrite()
it return true too. In gradle I have set
compileSdkVersion 23
,
minSdkVersion 23
,
targetSdkVersion 23
(I want to downgrade to minSdkVersion 21). In manifest I have:

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


Some code (updated):

File appDir = new File (Environment.getExternalStorageDirectory().getAbsolutePath()+"/Trainer app/");
File modelsDir = new File(appDir.getAbsolutePath()+"/Modele/");
File pdfDir = new File(appDir.getAbsolutePath()+"/Ostatnio wygenerowany trening/");
boolean result;
if(!appDir.exists()){
Log.wtf("StorageState",""+Environment.getExternalStorageState()+" Write: "+Environment.getExternalStorageDirectory().canWrite());
Log.wtf("TEST DIR","Create appDir "+result+" is dir: "+appDir.isDirectory());
Log.wtf("TEST DIR","Create appDir "+result);
MediaScannerConnection.scanFile(this, new String[]{appDir.toString()}, null, null);
}
//if(appDir.exists()&&!modelsDir.exists()){
if(!modelsDir.exists()){
//Log.wtf("MODELDIR", "" + modelsDir.mkdir() + " Path: " + modelsDir.getAbsolutePath());
result=modelsDir.mkdirs();
Log.wtf("TEST DIR", "Create modelDir "+result+" is dir: "+modelsDir.isDirectory());
MediaScannerConnection.scanFile(this, new String[]{modelsDir.toString()}, null, null);
}
//if(appDir.exists()&&!pdfDir.exists()){
if(!pdfDir.exists()){
//Log.wtf("PDFDIR", "" + pdfDir.mkdir() + " Path: " + pdfDir.getAbsolutePath());
result=pdfDir.mkdirs();
Log.wtf("TEST DIR","Create pdfDir "+result+" is dir: "+pdfDir.isDirectory());
MediaScannerConnection.scanFile(this, new String[]{pdfDir.toString()}, null, null);
}


UPDATE: now I have no right to write on external storage. When I change minSdk to 21 and targetSdk to 22 I have rights but I still can't cretae directories even using your tips

Answer

The problem lies right here

File appDir = new File (path,file_name);

File appDir = new File (Environment.getExternalStorageDirectory(),"Trainer app");

and this appDir.mkdir() will creates a file named Trainer app under the default path of storage directory

so you want to create directory like as Environment.getExternalStorageDirectory()/Trainer app then do it like this

String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/Trainer app/";

File dictionary = new File(path);
if (!dictionary.exists()) {
   dictionary.mkdirs();
}

To create a file later

File file1 = new File(dictionary, filename);