Problem when creating directory on SDCard. Android Studio

1

I have the following code, in which I want to create a directory (E4) in the SD external memory, however, when I run it I can not create it, and I also want it to be in / sdcard / documents /, but I put it on in ** / storage / emulated / 0 / ** Documents / and I can not find that folder anywhere

The code is as follows:

public static boolean isExternalStorageWritable() {
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        return true;
    }
    else
    {
        Log.e("isExtStorageWritable", "Can´t write to Ext. Storage");
        return false;
    }
}

//Get the path to the Documents Folder
public static File getStorageDir(String folderName) {
    // Get the directory for the user's public documents directory.
    File documents = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
    //path will be storage/sdcard/documents/foldername
    File path = new File(documents, folderName);

    Log.e("getStorageDir", path.toString());

    if (!path.mkdirs()) {
        Log.e("getStorageDir", "Directory not created");
    }
    else
    {            Log.e("getStorageDir", "Directory OK");
    }
    return path;
}

The LogCat errors I have to check give me the following:

E / getStorageDir: / storage / emulated / 0 / Documents / E4

E / getStorageDir: Directory not created

I am running it on Android 6.0 (in a Cubot Echo Music), however that same code does work as I want on an Android 5.0 from Xiaomi. I'm new to Android Studio

    
asked by Ángel Muñoz González 25.11.2017 в 23:11
source

1 answer

0

For versions higher than Android 5.1 it is necessary to request the permissions at run time, if the user does not grant the requested permission then the application will not be able to execute the code correctly.

Here is some information:

link

    
answered by 25.11.2017 / 23:14
source