Send a PDF file as Mail or offer app to directly v

2019-04-06 02:26发布

my Android 4+ app can create different reports in PDF format. Know I would like to offer the user the option to send the file via mail or to open in any app that can handle PDF files. Currently I use the following code:

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("application/pdf");

Uri uri = Uri.parse("file://" + reportFile.getAbsolutePath());
intent.putExtra(Intent.EXTRA_STREAM, uri);

try {
    startActivity(Intent.createChooser(intent, "Share PDF file"));
} catch (Exception e) {
    Toast.makeText(ReportsActivity.this, "Error: Cannot open or share created PDF report.", Toast.LENGTH_SHORT).show();
}

This workes fine, except only "send" apps are offerd like Bluetooth, Google Drive, E-Mail, etc. I installed the Acrobat Reader app which can of course view PDF files. But this app as well is only listed with "Send for signature" and not with "Open in Reader" or somthing like this.

I thought the ACTION_SEND intent would mean "send to other app" and not striktly "send somewhere else". What Intent can I use to include "open with" options as well?

1条回答
▲ chillily
2楼-- · 2019-04-06 03:03

What Intent can I use to include "open with" options as well?

ACTION_VIEW is for viewing files.

startActivity(new Intent(Intent.ACTION_VIEW).setDataAndType(Uri.fromFile(reportFile), "application/pdf")));

I thought the ACTION_SEND intent would mean "send to other app" and not striktly "send somewhere else".

No, ACTION_SEND is for sending things. That includes "send to yourself in another app" in some cases (e.g., sending to Google Drive), but it specifically is not for viewing files.

查看更多
登录 后发表回答