Create folder in Android directory

1

I did an APP that does the event monitoring, and finally, it generates a PDF report with the data of that event. But at the time of creating the file, if the device is running Android Nougat, nothing is generated. I have already tried to create a folder, save the file to folders that already exist, I looked at Android Developer, and nothing worked. How can I resolve this problem?

PS: In other versions it works normally.

try {

        filename = "CCB - Dados Reunião.pdf";

        document = new Document(PageSize.A4);

        path = Environment.getExternalStorageDirectory() + "/DADOS CCB";
        //path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS) + "/CCB";

        File dir = new File(path, filename);
        if (!dir.exists()) {
            dir.getParentFile().mkdirs();
        }

        FileOutputStream fOut = new FileOutputStream(dir);
        fOut.flush();

        PdfWriter.getInstance(document, fOut);
        document.open();} catch (DocumentException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        document.close();
        return true;
    }
    
asked by anonymous 10.07.2017 / 15:29

1 answer

1

I used this snippet for the user to give the permission at runtime.

    private final int PERMISSAO_REQUEST = 1;

No onCreate:

//USUARIA DAR A PERMISSAO PARA LER
    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
        if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_EXTERNAL_STORAGE)) {
        } else {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, PERMISSAO_REQUEST);
        }
    }

    //USUARIA DAR A PERMISSAO PARA ESCREVER
    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
        if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_EXTERNAL_STORAGE)) {
        } else {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSAO_REQUEST);
        }
    }
    
10.07.2017 / 17:36