mkdirs () do not create folders on my sd card


Good morning programmers ("the magicians"). I'm going to the following: I need to create files on the SD card, whenever I try to create it ends up creating it in the memory of the phone and not in the external SD. My code is as follows:

String file_path = Environment.getExternalStorageDirectory()+"/Psinapsis/Imagenes/Inventario"
File dir = new File(file_path);

    if (!dir.exists()) {
        Log.i(Constantes.TAG,"file_path existe NO");
        Log.i(Constantes.TAG,"dirTrue:" + dir.mkdirs());//devuelve FALSO :(

I have assembled the following permissions in the manifest:

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

I've also tried to generate the permissions at run time:

if (ContextCompat.checkSelfPermission(this,
            != PackageManager.PERMISSION_GRANTED) {

        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE)) {

            // Show an expanation to the user *asynchronously* -- don't block
            // this thread waiting for the user's response! After the user
            // sees the explanation, try again to request the permission.

        } else {

            // No explanation needed, we can request the permission.

                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},

            // app-defined int constant. The callback method gets the
            // result of the request.
    }else {
        Toast.makeText(getApplicationContext(),"Tengo permiso",Toast.LENGTH_SHORT).show();

What have I done wrong? :( obs: I already tried it with mkdir (), like nothing

asked by JoelRomero 09.03.2018 в 13:06

1 answer


There is no doubt that you have the required permission, this is not the problem.

In this case you have 2 external storage routes:

If we insert an SD Card, it will have the "primary" external storage and the storage in the SD Card, therefore two external storage directories, if you have more than one it indicates that you have an SD card mounted.

getExternalStorageDirectory returns true on phone without microsd

According to the above:

First check if you actually have an SDCard and it is mounted , for this you can use this method:

public static boolean hasRealRemovableSdCard(Context context) {
    return ContextCompat.getExternalFilesDirs(context, null).length >= 2;

If it exists then get the path so that in this path you save your files :

public static String getRemovableSDCardPath(Context context) {
    File[] storages = ContextCompat.getExternalFilesDirs(context, null);
    if (storages.length > 1 && storages[0] != null && storages[1] != null)
        return storages[1].toString();
        return "";

This would be an example according to your code:

    String file_path =  getRemovableSDCardPath(getApplicationContext()) +"/Psinapsis/Imagenes/Inventario";
    File dir = new File(file_path);
    if (!dir.exists()) {
        Log.i(TAG,"file_path NO existe, lo crea.");
        Log.i(TAG,"dirTrue:"  + dir.mkdirs());
answered by 09.03.2018 в 14:48