matejko219 matejko219 - 1 year ago 83
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.

return true for each direcotry and when I check
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()){"StorageState",""+Environment.getExternalStorageState()+" Write: "+Environment.getExternalStorageDirectory().canWrite());"TEST DIR","Create appDir "+result+" is dir: "+appDir.isDirectory());"TEST DIR","Create appDir "+result);
MediaScannerConnection.scanFile(this, new String[]{appDir.toString()}, null, null);
//"MODELDIR", "" + modelsDir.mkdir() + " Path: " + modelsDir.getAbsolutePath());
result=modelsDir.mkdirs();"TEST DIR", "Create modelDir "+result+" is dir: "+modelsDir.isDirectory());
MediaScannerConnection.scanFile(this, new String[]{modelsDir.toString()}, null, null);
//"PDFDIR", "" + pdfDir.mkdir() + " Path: " + pdfDir.getAbsolutePath());
result=pdfDir.mkdirs();"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 Source

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()) {

To create a file later

File file1 = new File(dictionary, filename);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download