Create folder in your device’s internal memory and display message after saving

Asked

Viewed 3,317 times

-1

I’m in two trouble.

The Toast in my method returns me the following error:

The method makeText(Context, Charsequence, int) in the type Toast is not applicable for the Arguments (Gerarrelatorio, String, int)

How to display a default error message? Like for example "Document not created".

To create the folder I am using this code, I tested in the emulator and nothing happened, creating the PDF file in the folder Documents with this line of right:

File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);

But only in the API 19, so I wanted to create the folder and check if it exists or not, so I could use in Android 4.0 onward.

public void CriarRelatorio(ClienteClass objCliente, EquipamentoClass objEquipamento, ServicoClass objServico, PecasClass objPecas){
    try {
        Document document = new Document();
        File direct = new File(Environment.getExternalStorageDirectory()+"/Relatorios");

        if(!direct.exists()) {
             if(direct.mkdir()); //se não existir o diretorio e criado
        }

        File pdffile = new File(direct, "RelatorioTeste.pdf");
        PdfWriter.getInstance(document, new FileOutputStream(pdffile));
        document.open();
        addMetaData(document);
        addTituloRelatorio(document);
        addConteudo(document);
        document.close();
        Toast.makeText(this, "Arquivo criado com sucesso", Toast.LENGTH_SHORT).show();
    } catch (Exception e) {
        // TODO: handle exception
    }
}
  • Could you check if an exception is thrown on that catch block? If so, post the stacktrace.

  • 1

    Your class GerarRelatorio is a Activity or Fragment?

  • Actually I had created only a published class with a method Generate Report, but in my example here I did, the method is within an Activity even, I thought I could call the method of creating the PDF by the main Activity.

1 answer

1


Try like this, here it works in all API.

  private File getDirFromSDCard() {
        if (Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {
            File sdcard = Environment.getExternalStorageDirectory()
                    .getAbsoluteFile();
            File dir = new File(sdcard, "APP_NAME" + File.separator + "PASTA_1");
            if (!dir.exists())
                dir.mkdirs();
            return dir;
        } else {
            return null;
        }
    }
  • very good the day but the line private File getDirFromSDCard() me returns this Syntax error on token "File", @ expected

  • What line ? remember that you have "APP_NAME" and the name of your application and "PASTA_1" name of the folder you will save the files.

Browser other questions tagged

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