简体   繁体   中英

How open document pdf/docx with external app in Android

图片截图

How open document pdf/docx with external app in Android

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM