Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot create directory in external storage although permissions are apparently set correctly

I have

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

in my manifest file, however I fail when trying to create a directory

    Log.d(LOG_STRING, android.os.Environment.getExternalStorageState() );
    java.io.File folder = new java.io.File(Environment.getExternalStorageDirectory() + java.io.File.separator + "test");
    boolean success = true;
    if (!folder.exists()) {
        success = folder.mkdir();
    }
    if (success) {
        Log.d(LOG_STRING, "Created directory");
    } else {
        Log.d(LOG_STRING, "FAILED WHILE CREATING DIRECTORY");
    }

The status of external storage is "mounted", but the test directory cannot be created and the output is "FAILED WHILE CREATING DIRECTORY".

Browsing in the phone to the "App Info", the permission "modify or delete the contents of your USB storage" is marked to be activated for my application.

What could be the cause of this? Some special setting of the phone? It's a Samsung GT-I9506 with Android 4.3 (API18). To be noted is that the getExternalStorageDirectory is not on the SD card, but on the internal storage (/storage/emulated/0/).


Update:

Speaking with colleagues, it seems that this device has undergone several tweaking after having been rooted (to allow a specific application to directly write on the SD card). It's probably not worth to investigate further, I will simply switch to another device. I'll keep the device for a while and if anybody will show up with an answer I will quickly test if it solves the problem.


Update 2: (Bounty end)

The problem remains unsolved, but as stated before, it is most likely something very specific to this one device. It's not possible to write on any path, being it external or internal storage, not even in the path returned by getExternalCacheDir().

like image 358
Antonio Avatar asked Oct 23 '15 14:10

Antonio


3 Answers

In my case I had to go under Settings -> Apps -> NameOfApp -> Permissions and activate the Storage permissions. I don't know why this comes deactivated by default if I have all the permissions on AndroidManifest.xml. I'm on a Moto G (3rd Gen) with Android Marshmallow 6.0.

like image 172
Mauricio Avatar answered Nov 10 '22 07:11

Mauricio


You first need to use file.mkdirs() instead of folder.mkdir().

And if it also not worked then you need to check if you can access the sdcard or not.

For checking you can use below method.

boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
String state = Environment.getExternalStorageState();

if (Environment.MEDIA_MOUNTED.equals(state)) {

    // We can read and write the media
    mExternalStorageAvailable = mExternalStorageWriteable = true;

} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {

    // We can only read the media
    mExternalStorageAvailable = true;
    mExternalStorageWriteable = false;

} else {

    // Something else is wrong. It may be one of many other states, but all we need
    //  to know is we can neither read nor write
    mExternalStorageAvailable = mExternalStorageWriteable = false;

}

Read at official document .

Hope it will give some clue to you.

like image 20
KishuDroid Avatar answered Nov 10 '22 06:11

KishuDroid


You can try this code. I have also emulated sd card on Lg g2 mini and I can only access external card in my package folder

for(File f : getExternalFilesDirs(null)){
    try {
        File f2 = new File(f.getAbsolutePath(), "testFile");

        Log.d("test log", "file path " + f2.getAbsolutePath());
        f2.createNewFile();
        Log.d("test log", "file exist " + f2.exists());
    } catch (IOException e) {
        Log.d("test log", "error");
    }
}

and output is:

D/test log? file path /storage/emulated/0/Android/data/my.awesome.package/files/testFile
D/test log? file exist true
D/test log? file path /storage/external_SD/Android/data/my.awesome.package/files/testFile
D/test log? file exist true
like image 3
jan hruska Avatar answered Nov 10 '22 07:11

jan hruska