How to open pdf with the default pdf reader?

1

I am new to android programming. I already implemented that my app generates a pdf document, and it creates everything well. The problem, I want to send a Snackbar with the action to open it in another app (pdf reader) all right up there, the question is that when you open it, it comes out so I can put a password ??? How to solve this problem, clarify that only comes out of the password when I send it open from my app, that is, if I try to open it from the file browser, it opens up well. This is the method with which I try to open the pdf

private void notificacionPDFCreado(final String nombPDF) {
    Snackbar.make(linearLayout_totales, "PDF Creado Satisfactoriamente", Snackbar.LENGTH_INDEFINITE)
            .setActionTextColor(Color.parseColor("#ffff7663"))
            .setAction("Ver", new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Log.i("Snackbar", "Pulsada acción snackbar!");
                    File file = new File(Environment.getExternalStorageDirectory() + "/" + Environment.DIRECTORY_DOCUMENTS,nombPDF);
                    Uri path = Uri.fromFile(file);
                    Log.i("Snackbar", "" + path);
                    Intent pdfOpenintent = new Intent(Intent.ACTION_VIEW);

                    pdfOpenintent.setDataAndType(path, "application/pdf");

                    pdfOpenintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

                    //Intent intent = Intent.createChooser(pdfOpenintent,"Abrir con...");
                    try {
                        startActivity(pdfOpenintent);
                        Toast.makeText(DeudasCobrar.this, "Abriendo PDF " + nombPDF, Toast.LENGTH_SHORT).show();
                    } catch (ActivityNotFoundException e) {
                        Log.i("Snackbar", "Error al iniciar activity");

                    }
                }
            })
            .show();

}
    
asked by Droktar 07.11.2017 в 19:41
source

1 answer

1

I already found the solution, I had it in front of me all the time, I was trying to access it from the root of the device and that is why I placed the file as read only and with a password.

//Procedimiento para mostrar el documento PDF generado
public void mostrarPDF(String nombPdf, Context context) {
    Toast.makeText(context, "Visualizando documento", Toast.LENGTH_LONG).show();

    // Así va correctamente la dirección 
    String dir = Environment.getExternalStorageDirectory()+ "/Mi App/pdf/" + nombPdf;

    File arch = new File(dir);

    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.fromFile(arch), "application/pdf");
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    try {
        context.startActivity(intent);
    } catch (ActivityNotFoundException e) {
        Toast.makeText(context, "No existe una aplicación para abrir el PDF", Toast.LENGTH_SHORT).show();
    }
}
    
answered by 07.12.2017 / 15:45
source