0

Puedo generar un pdf en el emulador de android studio, pero cuando lo pruebo en un celular físico me sale un error.

File file = new File(Environment.getExternalStorageDirectory(), "GFG.pdf");

    try {
        pdfDocument.writeTo(new FileOutputStream(file));
        Toast.makeText(getContext(), "Archivo PDF generado correctamente.", Toast.LENGTH_SHORT).show();
    } catch (IOException e) {
        Toast.makeText(getContext(), "Error PDF." + e, Toast.LENGTH_SHORT).show();
        e.printStackTrace();
    }

    pdfDocument.close();

Error: error pdf java.io.filenotfoundexception storage emulated

1 respuesta 1

0

El error indica que el archivo no existe:

error pdf java.io.filenotfoundexception storage emulated

pero en realidad el problema es el lugar donde actualmente Android permite crear el archivo, actualmente ya no debes usar Environment.getExternalStorageDirectory() , debes usar getExternalFilesDir() :

//File file = new File(Environment.getExternalStorageDirectory(), "GFG.pdf");
File file = new File(getExternalFilesDir(null).getAbsolutePath(), "GFG.pdf");
1
  • getExternalFilesDir me tira este error Cannot resolve method 'getExternalFilesDir' el 5 jul. 2022 a las 14:50

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.