Create folder in Android directory

Asked

Viewed 1,983 times

1

I made an APP that monitors events, and finally generates a PDF report with the data of this event. But when creating the file, if the device is with Android Nougat, nothing is generated. I’ve tried to create a folder, save the file in folders that already exist, looked at Android Developer, and nothing worked. How can I solve this problem?

PS: In other versions 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;
    }

1 answer

1


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

    private final int PERMISSAO_REQUEST = 1;

In 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);
        }
    }

Browser other questions tagged

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