Open pdf document via Intent?

Asked

Viewed 921 times

0

I have an application that generates a pdf through a button and when clicking on the button opens a alertdialog where I name my file and therefore opens a snackbar with the title " Do you want to open the file ?" and a "yes" button and when clicking it opens the download directory and not the Documents and when clicking on some PDF it does not open and return to the application. What can it be ?

Follow snippet of the snackbar button:

 Snackbar snackbar = Snackbar.make(constraintLayout,"Deseja abrir arquivo :", Snackbar.LENGTH_INDEFINITE);
        snackbar.setAction(" ", new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                String pdf = nomepdf.getText().toString();
                File file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);

                Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
                intent.setDataAndType(Uri.parse(String.valueOf(file+"/"+pdf+".pdf")), "application/pdf");
                intent.addCategory(Intent.CATEGORY_OPENABLE);


                Intent chooser = Intent.createChooser(intent, "Open File !");
                startActivity(chooser);


            }

        });

1 answer

-1

Remove setDataAndType Add Uri and type separately as follows:

Intent.setType("application/pdf") // identifica o tipo de arquivo que vc quer compartilhar, isso permite que a lista de programas exibidas seja correta(so exibira apps capazes de lerem pdf
Intent.putExtra(EXTRA_STREAM, uri) //adiciona o arquivo a ser compartilhado
Intent.setAction(ACTION_SEND)
Intent.setFlags(FLAG_GRANT_READ_URI_PERMISSION)
  • 1

    You can enrich your answer by explaining what these lines of code are for.

  • Continued the same way, why will be ?

  • I edited the answer maybe it’ll help

  • I think it worked, but when running Intent chooser, I get the message that PD reading applications have been blocked, disabled or not installed. The native android player does not work and the reader I have installed also did not work.

Browser other questions tagged

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