1

I have a problem. I am trying to change names of my app's 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 directory.

When I check Environment.getExternalStorageDirectory().canWrite() it return true too. My gradle configuration

  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 create directories even using your tips

Cœur
  • 37,241
  • 25
  • 195
  • 267
matejko219
  • 1,601
  • 10
  • 14

1 Answers1

1

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);

Update : You need to implement Run-Time permission model to gain the access from user to access storage , try this link for implementation

Community
  • 1
  • 1
Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68