Open PDF stored in external directory from android studio

1

Through JSON I get some PDFs that I found on base 64 and I keep them in the following way:

OutputStream out = new FileOutputStream(Environment.getExternalStorageDirectory().toString() + "/documentosConsumerTemporal/pdfConsumer_" + oNombreDocumentoMatriz + ".pdf");
                                    out.write(Base64.decode(oDatosDocumentoMatriz, Base64.DEFAULT));
                                    out.flush();
                                    out.close();

And it saves it in the following way:

/storage/sdcard0/documentosTemporal/pdf_SPV.pdf

Then, from another activity I want to launch those PDF's in the following way:

    Intent intent = new Intent(Intent.ACTION_VIEW);

        intent.setDataAndType(Uri.parse(Environment.getExternalStorageDirectory().toString() + "/documentosConsumerTemporal/pdfConsumer_SPV.pdf"), "application/pdf");
        try {
            startActivity(intent);
        } catch (ActivityNotFoundException e) {
            Toast.makeText(ActivityDatosNumeroOperacion.this, Environment.getExternalStorageDirectory().toString() + "/documentosTemporal/pdf_SPV.pdf", Toast.LENGTH_LONG).show();
        }

but I get the following error message:

La ruta de acceso al documento no es valida

I have tried saving in the path of the same application with the following code:

OutputStream out = new FileOutputStream(getFilesDir() + "/pdf_" + oNombreDocumentoMatriz + ".pdf");

But he gives me the same message, which he does not find.

I have tried to open the PDF directly and if it opens, but by code no. What can it be? Permissions are OK, do I have to put them in an already established folder?

Thanks

    
asked by Kenny 11.08.2017 в 18:17
source

1 answer

1

Try this way:

File file = new File(Environment.getExternalStorageDirectory().toString() + "/documentosConsumerTemporal/pdfConsumer_SPV.pdf");
    Intent target = new Intent(Intent.ACTION_VIEW);
    target.setDataAndType(Uri.fromFile(file),"application/pdf");
    target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);


    Intent intent = Intent.createChooser(target, "Abrir PDF");
    try {
        startActivity(intent);
    } catch (ActivityNotFoundException e) {
        // el usuario no tiene ninguna app que pueda abrir pdfs
    }

or instead of getExternalStorageDirectory().toString() change to getExternalStorageDirectory().getAbsolutePath()

    
answered by 11.08.2017 / 20:42
source