4

I am trying to create an intent that will start the MMS application for me with an image file attached and some pre-defined text present in the message body.

Thus far I've been able to accomplish either or, but not both at the same time.

Things I've tried (with their results):

sendIntent = new Intent(android.content.Intent.ACTION_SEND,Uri.parse("mms://"));
sendIntent.setType("image/gif");
sendIntent.putExtra(Intent.EXTRA_STREAM, imgStreamUri);
sendIntent.putExtra("sms_body", "HelloWorld");
startActivity(Intent.createChooser(sendIntent,"Send"));    
/**********
Image file is attached but no text added to message body.
 **********/

sendIntent = new Intent(android.content.Intent.ACTION_SEND);
sendIntent.setType("image/gif");
sendIntent.putExtra(Intent.EXTRA_STREAM, imgStreamUri);
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "HelloWorld");
sendIntent.putExtra(Intent.EXTRA_TITLE, "WorldHello");
startActivity(Intent.createChooser(sendIntent,"Send"));
/**********
Image file is attached but no text added to message body(or subject or anything).
 **********/

Does anyone know how I can attach both body text and an image file to an mms intent that will launch the default messaging application with the appropriate items filled in?

EDIT: Tested the code @lenik provided in answer. It is working on some devices, here's what I found

Works correctly:

  • Epic 4g (Galaxy S)
  • Epic 4g Touch (Galaxy S II)
  • Galaxy Nexus(ICS 4.0.4)
  • HTC Desire (Froyo 2.2)
  • Motorola Photon

Image attached but no text:

  • Sidekick 4g
  • Samsung Transform Ultra

Anyone know if I am basically s.o.l. on the devices that don't work properly this way?

FoamyGuy
  • 46,603
  • 18
  • 125
  • 156

1 Answers1

8

The following code works for me:

Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra("sms_body", "Hi how are you");
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File("/sdcard/file.gif")));
intent.setType("image/gif"); 
startActivity(Intent.createChooser(intent,"Send"));
lenik
  • 23,228
  • 4
  • 34
  • 43
  • I get no text added. What device did you run on? – FoamyGuy May 08 '12 at 02:22
  • works on my HTC Desire (Froyo 2.2) and Galaxy Nexus (ICS 4.0.4). – lenik May 08 '12 at 03:05
  • please, try to use my sorce **verbatim**, most probably the problem is related to your `imgStreamUri` parameter, if it has wrong contents, the text or file don't show properly in MMS message. – lenik May 08 '12 at 05:21
  • I've just been able to test on a wider range of devices. See my edit for results. – FoamyGuy May 08 '12 at 13:34
  • @lenik how to attached audio file in mms programmatically. – Harshid Dec 31 '12 at 12:07
  • @Harshid I think same way, just supply the proper file name and change MIME type (`intent.setType()`) accordingly – lenik Jan 20 '13 at 10:09
  • @lenik i am attach image from mipmap instead of SD card the message is showing "You can't add this picture in message" shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("android.resource://" + getPackageName() + "/" + R.mipmap.ic_launcher)); – M.Muzammil Jun 28 '16 at 05:58