简体   繁体   English

如何在 Android 中使用外部应用程序打开文档 pdf/docx

[英]How open document pdf/docx with external app in Android

图片截图

How open document pdf/docx with external app in Android如何在 Android 中使用外部应用程序打开文档 pdf/docx

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注意:您需要 File Provider 来授予 URI 权限检查这个

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您将在 onActivity 结果中得到结果

//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有关更多信息,您可以查看https://stackoverflow.com/a/11038348/11834065

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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