Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send a PDF file as Mail or offer app to directly view the file

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?

like image 259
Andrei Herford Avatar asked Sep 04 '14 19:09

Andrei Herford


1 Answers

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.

like image 155
CommonsWare Avatar answered Sep 23 '22 15:09

CommonsWare