mkdir on android does not work

Asked

Viewed 83 times

0

I’m trying to create a folder on Android to save the photos of the vouchers, but I’m not succeeding. I’ve given permission in Manifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="<meu.package>">

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

and, according to the new rules of Android 7.0, I am requesting these permissions at runtime.

The code goes like this:

 private void CriaPasta() {
        pasta = Environment.getExternalStorageDirectory().getPath() + "/Android/data/" + getApplicationContext().getPackageName() + "/comprovantes/";
        File folder = new File(pasta);

        if (!folder.exists()) {
            if (!folder.mkdir()) ;
            Toast.makeText(this, pasta + " não pode ser criada.", Toast.LENGTH_SHORT).show();
        }
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case WRITE_EXTERNAL_STORAGE: {
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    CriaPasta();
                }
            }
        }
    }

Here in the Updateview method of this Activity I check if I already have permission and try to create the folder if it does not exist. Here the stream goes straight to the Creatifolder routine because this permission has already been requested in the main entry of the program.

    private void UpdateView() {

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

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

But in the Create routine the message saying that the folder cannot be created occurs every time.

This is part of the build.Radle file.

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.2"
    defaultConfig {
        applicationId "<meu.package>"
        minSdkVersion 17
        targetSdkVersion 26

It seems to have to do with this version, because in other applications my creation of similar folders usually occurs on the same device.

By the way, when I install the other apps Android creates the folder Emulated/0/Android/data/ automatically and in this application it does not happen.

  • 1

    The only reason I can’t find is that the directory is not accessible. You can check using getExternal(). Anyway, from version 19 (Android 4.4) should not use this method. When dealing with the External Storage keep in mind what is said in this reply.

1 answer

1


There is a more natural method to reach the directory where you are trying to create your folders:

Context.getExternalFilesDir(@Nullable String type)

Returns the absolute path to the directory on the primary shared/external storage device where the application can place persistent files of your property. These files are internal applications and are usually not visible to the user as media.

This method returns a File pointing to /memorInterna/Android/data/name.do.package/files

So try to replace it this way:

private void CriaPasta() {
    File pasta = new File(getExternalFilesDir(null), "comprovantes");
    /*
       /memóriaInterna/Android/data/nome.do.pacote/files/comprovantes
    */
    if (!pasta.exists()) {
        if (!pasta.mkdirs()) { // <- mkdirs()
            Toast.makeText(this, pasta + " não pode ser criada.", Toast.LENGTH_SHORT).show();
        }
    }
}

Notice that I used mkdirs() that creates the folder, including any parent directory needed, but non-existent.

  • Perfect Lennoard. Worked correctly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.