9

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?

Andrei Herford
  • 17,570
  • 19
  • 91
  • 225

1 Answers1

15

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thank you very much for the hint the clarification. Is there any way to combine these two to let the user choose to open OR send the file? Of course I use a second dialog to ask first if the send options or the view option should be offered. But this would be on extra step for the user. Additionally it would be bad to ask "Open or View" before I know if any viewing app is available... – Andrei Herford Sep 05 '14 at 06:01
  • @AndreiHerford: "Is there any way to combine these two to let the user choose to open OR send the file?" -- I wouldn't recommend that, as that's not very typical of other Android apps. That being said, you can try experimenting [with `EXTRA_INITIAL_INTENTS`](http://developer.android.com/reference/android/content/Intent.html#EXTRA_INITIAL_INTENTS). "Of course I use a second dialog to ask first if the send options or the view option should be offered" -- IMHO, there should be two separate UI triggers for each, neither of which is named "open". "View" and "share" are the proper Android verbs. – CommonsWare Sep 05 '14 at 10:32
  • Works perfectly to launch installed apv pdf reader to display a PDF file. Thanks. – Hong Mar 31 '16 at 18:06