4

In my Android App I send email messages with images attached.

Using the Intent system to send it, I can do one of the following two things:

1) Specify type as "message/rfc822" so that ONLY email applications are shown in the Chooser.

Inconvenience: I cannot specify the mime type of the image I attach using EXTRA_STREAM and a Uri. Many receiving email apps (Gmail, Android, etc) show this as an unknown binaru "blob" attached to the message, don't know how to preview it and don't know how to open it as an attachment.

2) Specify the type as (say) "image/png". The image is attached and email clients such as Gmail can preview it, and open the attachment in the appropriate application.

Inconvenience: For the sending user, I cannot reduce the list of apps the user has to select from in the Chooser to email apps, and MANY apps are shown in my Android device, most of which are not email apps and not what I want.

Is there anyway to specify its a "message/rfc822" email Intent AND to specify the MIME type of the data attached via Uri in the Intent.EXTRA_STREAM?

BTW: I am providing the file from my own ContentProvider and the getType() method (used to determine file MIME type) is NOT being called. The query() method is but doesn't request the file type, only display name and file size.

thanks

Andrew Mackenzie
  • 5,477
  • 5
  • 48
  • 70

1 Answers1

3

Cross-posting my answer from the android-developer Google Group:

If you are willing to roll your own dialog, you could:

Step #1: Create the message/rfc822 Intent, as if you were going to send that way, and use it with PackageManager and queryIntentActivities() to find out who handles it.

Step #2: Create the image/png Intent, as if you were going to send that way, and use it with PackageManager and queryIntentActivities() to find out who handles it.

Step #3: Compute the intersection of those two sets of activities.

Step #4: Use those to populate an AlertDialog for the user to choose from.

  • Step #4a: If the intersection has one match, skip this step.
  • Step #4b: If the intersection has zero matches, let the user know you can't send the message.

Step #5: Modify the image/png Intent to add the component selected from the dialog, and call startActivity() on it.

By specifying the component in the Intent, it will go to that particular activity. This is effectively what the regular chooser does.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks. I may go down that path (if there is not other way) when I have some more time. It's slightly annoying that the BlueTooth activity always shows up in the chooser list to send an email and I'd also filter that out. BTW: I have implemented the query() method of my own content provider to provide DISPLAY_NAME and SIZE to the (email) app that is going to send the email. I have found that if I add a valid image file extension (e.g. ".png") onto the end of the display name then most email clients will preview it and open an image viewer to view the attachment. Not fool-proof but helps. – Andrew Mackenzie Apr 30 '11 at 14:57