mkdir on android does not work [duplicate]

0

I'm trying to create a folder on Android to save the photos of the vouchers, but I'm not having success. I already gave 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 Android 7.0 rules, I'm requesting these permissions at runtime.

The code looks 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 flow goes straight to the CreatePaste routine because that permission has already been requested at 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 CreateCreate routine the message saying that the folder can not be created occurs every time.

This is part of the build.gradle 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 the creation of similar folders usually occurs on the same machine.

Incidentally, when I install the other applications Android creates the folder emulated / 0 / Android / data / automatically and in this application this does not happen.

    
asked by anonymous 13.07.2018 / 14:04

1 answer

1

There is a more natural way 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 shared / external primary 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 by pointing to /ememory/Android/data/name.of.pacote/files

Then try to replace it like this:

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 necessary parent directory, but nonexistent.

    
13.07.2018 / 14:52