0

image screehoot

How open document pdf/docx with external app in Android

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • 1
    Possible duplicate of [How to open a PDF via Intent from SD card](https://stackoverflow.com/questions/10530416/how-to-open-a-pdf-via-intent-from-sd-card) – Rasel Oct 09 '19 at 04:24

2 Answers2

0

Try the below code :

 File pdfFile = new File("File Path");
 Intent openPdf = new Intent(Intent.ACTION_VIEW);
 openPdf.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
 fileUri = FileProvider.getUriForFile(viewContext,com.mydomain.fileprovider, pdfFile);
 openPdf.setDataAndType(fileUri, "application/pdf");
 openPdf.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 startActivity(Intent.createChooser(openPdf, "Select Application"));

Note : You need File Provider for granting URI permissions check this out

0

This will help you

//method to show file chooser
    private void showFileChooser() {
        Intent intent = new Intent();
        intent.setType("application/pdf");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "Select Pdf"), PICK_PDF_REQUEST);
    }

You will get the result in onActivity result

//handling the image chooser activity result
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == PICK_PDF_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
            filePath = data.getData();
        }
    }

For more info you can look into https://stackoverflow.com/a/11038348/11834065