Friday 8 March 2013

How to send an MMS Programmatically in Android

To send an MMS in Android , we'll use intent . Here is the code snippet.

Android Layout File

Layout File Name :- activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/et_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="39dp"
        android:layout_marginTop="22dp"
        android:ems="10"
        android:hint="Enter Number"
        android:inputType="number"
        android:singleLine="true" />

       

    <EditText
        android:id="@+id/et_msg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/et_number"
        android:layout_below="@+id/et_number"
        android:layout_marginTop="24dp"
        android:ems="10"
        android:lines="3"
        android:hint="Enter Message Body" />

    <Button
        android:id="@+id/btn_send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/et_msg"
        android:layout_centerVertical="true"
        android:text="Send MMS" />

</RelativeLayout>






*************************** .java File ********************

Class Name:- MainACtivity.java

Package Name:-  com.dummymms

package com.dummymms;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity implements OnClickListener {
   
    Button btnmms;
    EditText etnum, etmsg;
   

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        etnum=(EditText)findViewById(R.id.et_number);
        etmsg=(EditText)findViewById(R.id.et_msg);
        btnmms=(Button)findViewById(R.id.btn_send);
        btnmms.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    @Override
    public void onClick(View v) {
        try {
             //Uri uri = Uri.parse("content://media/external/images/media/1");
           

        //     Uri uri = Uri.parse("file://mnt/sdcard/test.jpg");
           

           
            Uri uri = Uri.parse("file://"+Environment.getExternalStorageDirectory()+"/test.png");
            Intent i = new Intent(Intent.ACTION_SEND);
            i.putExtra("address",etnum.getText().toString());
            i.putExtra("sms_body",etmsg.getText().toString());
            i.putExtra(Intent.EXTRA_STREAM,"file:/"+uri);
            i.setType("image/png");
            startActivity(i);
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
       
    }

}






Make sure to add permission in the Manifest File


<uses-permission android:name="android.permission.RECEIVE_MMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />



Download the source code here

19 comments:

  1. hi
    can u send the MMS full source code to my mail.am getting errors with the above code
    thanks

    Ash

    ReplyDelete
  2. hi......

    This Ganesh u r blog is awesome....this is very useful for me.i am new beginner of Android
    can u plzzz...send me MMS App full source code to my gmail id...

    Thanks
    Ganesh

    ReplyDelete
  3. The above crashed because it does not found any image with the name of "text.png".
    You need to add an image in emulator or device with the name of "test.png" and than click on send button.You'll be also to send an MMS.

    Note :- While running on device make sure you have activated GPRS pack on your cell phone.

    ReplyDelete
  4. Ganesh : You can download the above code from link provided below the code.

    ReplyDelete
  5. hi
    can u provide the source code for MMS with image as u shown in the UI plz. i added image but still am not getting image in emulater.plz send it my mail...

    Thanks

    ReplyDelete
  6. Ashok :- For testing on Device you have to activate GPRS , although it work fine on emulator for Android version 2.2 and above.
    Download the code from here :-
    http://www.mediafire.com/?vwb48xnizl2882j

    ReplyDelete
    Replies
    1. Sumit: when i run the application on my htc device. on click of "Send MMS" button, it will ask "Complete action using". Why this popup appear...

      Thanks

      Delete
    2. Hi Rahul,

      I had used the android default interface to send an MMS using intent I had pass the values like number, text and data to it, Afterwards android will take care of it.

      Delete
    3. Hi Sumit,

      I want to send video file through MMS after clicking on one button.I don,t want to see any interface to user for sending MMS. All should be done without user interaction.

      Delete
  7. thankyu very much sir,... this is nice post...

    ReplyDelete
  8. As i can see.. The implicit intent is being called which navigates to android native messaging app... Can you provide the code for sending MMS in background which will run without user interaction?

    ReplyDelete
  9. i'm chanru i need source code for send mms without intent chandbecse@gmail.com
    any one know the code please send me the code to my mail id.

    ReplyDelete
  10. Hi! I though it will send MMS on its own process, why it is needed to complete the action using different application? just a little ask, can you make it whithout completing the action using different application? i am new in android developing and i want to learn how mms is working and how it can be done in code, i think this blog can help me a lot for that. thanks!

    ReplyDelete
  11. and one thing, so i already add some text in "message body", and after i click the send mms button it asking me which application do i want to continue the process, if i select my default messenger, my text in "message body" doesn't appear on my messenger, so it’s nonsense to type in “message body”. Thanks!

    ReplyDelete
  12. Sir, I downloaded the code. But when u unzip it says the file is corrupt. Could u please mail me the full code?

    ReplyDelete
  13. I want to send SMS from default client to any default SMS/MMS client. If my app is installed, it should executed it and pass a string variable. If not, it execute the default browser and passes an HTML msg.
    Can I do that w/ multipart SMS? Can I even do it w/ MMS?
    Thanks for your advice.

    ReplyDelete
  14. hello I tried your code, but the image is not able send

    ReplyDelete
    Replies
    1. and can we have code without Intent ? means I want to send mms from my app only. not to use other apps to send mms

      Delete
  15. Hello,

    Thanks for this code.

    I tried the project as is. But it sends a SMS instead a MMS.

    Photo is not attached. I uploaded load into the mediafile device folder a photo named "test.jpg". So, I renamed .../test.png") into "test.jpg"

    When I test the application, only a SMS (not a MMS as I would like) is sent.

    Any idea why ?

    Regards,
    Eric.

    ReplyDelete