adams172
adams172

Reputation: 189

Create directory in /sdcard/Pictures

I'm trying to create directory under /storage/emulated/0/Pictures with this code:

        File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath() + "/newDirectory");

        boolean success = false;
        if(!outputDir.exists()){
            success = outputDir.mkdirs();
        }
        if(success == true){
            Log.d(TAG, "file created");
        }

I can't understand what's wrong because I don't get any error or exception at this stage but directory isn't created. Can anywone help?

Upvotes: 0

Views: 59

Answers (2)

zed
zed

Reputation: 3287

How about you try to log file.getAbsolutePath(); to see if the path is right?

Second, your code is not complete because your first line is an error.

Try this:

File directory = new File(Environment.getExternalStorageDirectory(Environment.DIRECTORY_PICTURES)+File.separator+"pictures");
directory.mkdirs();

Make sure your permissions are set.

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

Upvotes: 0

rafsanahmad007
rafsanahmad007

Reputation: 23881

try this:

 String folder_root = "Pictures";

File f = new File(Environment.getExternalStorageDirectory(), folder_root);
if (!f.exists()) {
    f.mkdirs();
}

File f1 = new File(Environment.getExternalStorageDirectory() + "/" + folder_root, "yourDirectoryName");
if (!f1.exists()) {
    f1.mkdirs();
}

In your manifest add permission

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

For Api>23 you also need runtime permisssion

ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);

Upvotes: 2

Related Questions