0

This question is duplicate or triplicate, but I can't create a folder! I've read other questions on this site. I have added the permission in the manifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
        package="com.example.val.scemo">
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>`

My code is this (I tried everything, I have copied the code from the answers on this site):

File logFile = new File(LOG_PATH);
boolean a = false;
a = logFile.getParentFile().mkdirs();
Log.d("Creare: ", "Directory creata? " + a);
 ///////////////////////////////////////////////////
File folder = new File(Environment.getExternalStorageDirectory() + File.separator + "TollCulator");
boolean success = true;
if (!folder.exists()) {
    Log.d("Creare: ", "Riprovo ");
    success = folder.mkdir();
    Log.d("Creare: ", "Fatto ");
}
if (success) {
    Log.d("Creare: ", "Cartella creata!1 ");
    // Do something on success
} else {
    Log.d("Creare: ", "FAIL1 ");
    // Do something else on failure
}

/////////////////////////////////////////////////////

folder = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Example");
boolean success1 = true;
if (!folder.exists()) {
    success1 = folder.mkdirs();
}
if (success1) {
    Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_LONG).show();
    Log.d("Creare: ", "Cartella creata!2 ");
} else {
    Toast.makeText(getApplicationContext(), "Failure", Toast.LENGTH_LONG).show();
    Log.d("Creare: ", "FAIL2 ");
}

All fails. Where am I wrong? Sorry for my English.

Christine
  • 5,617
  • 4
  • 38
  • 61
valeriot90
  • 65
  • 7
  • Are you getting an exception or is the code executing and you can't find the file? – Robert Conley Nov 27 '15 at 18:25
  • When the code executing i can't find the folder/directory. – valeriot90 Nov 28 '15 at 10:20
  • I have tried this code File file = new File (Environment.getExternalStorageDirectory().getPath() + File.separator + "FolderName"); if (!file.isDirectory ()) { file.mkdirs(); } but don't work. – valeriot90 Nov 28 '15 at 10:24
  • I tried another source code from another project to work properly, but on my Nexus 5 does not create the folder. The code is this: private static string logFilePath = null; logFilePath = Environment.getExternalStorageDirectory() + File.separator + "SleepMonitorLogs" + File.separator + "blablafile.txt"; File logFile = new File(logFilePath); boolean a = false; a = logFile.getParentFile().mkdirs(); Log.d("DIRECTORY ", "CREATA? " + a); My Nexus 5 is upgraded to 6.0, no root, no changes. – valeriot90 Nov 28 '15 at 10:58

3 Answers3

0

Try the following:

File file = new File (Environment.getExternalStorageDirectory().getPath() + File.separator + "FolderName");
if (!file.isDirectory ()) {
    file.mkdirs();
}
mWhitley
  • 453
  • 7
  • 14
0

SOLUTION I have changed a version of sdk target, from 23 to 21. Now there is another error: Error:(2) Error retrieving parent for item: No resource found that matches the given name 'android:Widget.Material.Button.Colored'. in build.grandle in the line: compile 'com.android.support:support-v4:23.0.1' i have restored at sdk 23 i have sync the project and now working properly.

valeriot90
  • 65
  • 7
0

This is due to Runtime Permission in Android 6.0 Marshmallow. WRITE_EXTERNAL_STORAGE falls under dangerous permissions list. We need to check permission before every read/write operation.

int permissionCheck = ContextCompat.checkSelfPermission(this,
            Manifest.permission.WRITE_EXTERNAL_STORAGE);

For requesting permissions at runtime checkout these links : https://developer.android.com/training/permissions/requesting.html

Arpit Patel has explained it very well here : https://stackoverflow.com/a/37358852/5460053

And your are not getting any exception as File.mkdir() and File.mkdirs() does not throw IOException on failure.

Excerpt from the docs : File.mkdir() does not throw IOException

More details : https://developer.android.com/reference/java/io/File.html#mkdir()

Community
  • 1
  • 1
Monish Kamble
  • 1,478
  • 1
  • 15
  • 28