48

I am working on a multimedia application. I am capturing one image through the camera and want to send that image with a text to some other number. But I am not getting how to send the image via the MMS.

Alan
  • 3,715
  • 3
  • 39
  • 57
Sanjay
  • 481
  • 1
  • 5
  • 3

4 Answers4

40

MMS is just a htttp-post request. You should perform the request using extra network feature :

final ConnectivityManager connMgr = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
final int result = connMgr.startUsingNetworkFeature( ConnectivityManager.TYPE_MOBILE, Phone.FEATURE_ENABLE_MMS);

If you get result with Phone.APN_REQUEST_STARTED value, you have to wait for proper state. Register BroadCastReciver and wait until Phone.APN_ALREADY_ACTIVE appears:

final IntentFilter filter = new IntentFilter();
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
context.registerReceiver(reciver, filter);

If connection background is ready, build content and perform request. If you want to do that using android's internal code, please use this:

final SendReq sendRequest = new SendReq();
    final EncodedStringValue[] sub = EncodedStringValue.extract(subject);
    if (sub != null && sub.length > 0) {
        sendRequest.setSubject(sub[0]);
    }
    final EncodedStringValue[] phoneNumbers = EncodedStringValue
            .extract(recipient);
    if (phoneNumbers != null && phoneNumbers.length > 0) {
        sendRequest.addTo(phoneNumbers[0]);
    }

    final PduBody pduBody = new PduBody();

    if (parts != null) {
        for (MMSPart part : parts) {
            final PduPart partPdu = new PduPart();
            partPdu.setName(part.Name.getBytes());
            partPdu.setContentType(part.MimeType.getBytes());
            partPdu.setData(part.Data);
            pduBody.addPart(partPdu);
        }
    }

    sendRequest.setBody(pduBody);

    final PduComposer composer = new PduComposer(this.context, sendRequest);
    final byte[] bytesToSend = composer.make();

    HttpUtils.httpConnection(context, 4444L, MMSCenterUrl,
            bytesToSendFromPDU, HttpUtils.HTTP_POST_METHOD, !TextUtils
                    .isEmpty(MMSProxy), MMSProxy, port);

MMSCenterUrl: url from MMS-APNs, MMSProxy: proxy from MMS-APNs, port: port from MMS-APNs

Note that some classes are from internal packages. Download from android git is required.

The request should be done with url from user's apn-space...code..:

public class APNHelper {

public class APN {
    public String MMSCenterUrl = "";
    public String MMSPort = "";
    public String MMSProxy = ""; 
}

public APNHelper(final Context context) {
    this.context = context;
}   

public List<APN> getMMSApns() {     
    final Cursor apnCursor = this.context.getContentResolver().query(Uri.withAppendedPath(Telephony.Carriers.CONTENT_URI, "current"), null, null, null, null);
if ( apnCursor == null ) {
        return Collections.EMPTY_LIST;
    } else {
        final List<APN> results = new ArrayList<APN>(); 
            if ( apnCursor.moveToFirst() ) {
        do {
            final String type = apnCursor.getString(apnCursor.getColumnIndex(Telephony.Carriers.TYPE));
            if ( !TextUtils.isEmpty(type) && ( type.equalsIgnoreCase(Phone.APN_TYPE_ALL) || type.equalsIgnoreCase(Phone.APN_TYPE_MMS) ) ) {
                final String mmsc = apnCursor.getString(apnCursor.getColumnIndex(Telephony.Carriers.MMSC));
                final String mmsProxy = apnCursor.getString(apnCursor.getColumnIndex(Telephony.Carriers.MMSPROXY));
                final String port = apnCursor.getString(apnCursor.getColumnIndex(Telephony.Carriers.MMSPORT));                  
                final APN apn = new APN();
                apn.MMSCenterUrl = mmsc;
                apn.MMSProxy = mmsProxy;
                apn.MMSPort = port;
                results.add(apn);
            }
        } while ( apnCursor.moveToNext() ); 
             }              
        apnCursor.close();
        return results;
    }
}

private Context context;

}
Maveňツ
  • 1
  • 12
  • 50
  • 89
Damian Kołakowski
  • 2,731
  • 22
  • 25
  • 5
    Can someone explain how to properly get all the referenced code into Eclipse (e.g. the SendReq class, etc)? I have git and repo and have the android source and have the MMS project itself set up in Eclipse (I found it in the SDK at /packages/apps/MMS). However, it references lots of other parts of the Android system, and I don't know how to get all those references into my project in Eclipse without copying files into my project's src directory and proper subdirectories by hand, which is overwhelming. – Tyler Collier Mar 16 '11 at 02:27
  • 1
    A lot of these classes seem to come from the non-public google APIs (cfr: Mms application). I don't think they can be used out of the box. – ddewaele Mar 27 '11 at 10:19
  • totally works! much kudos! (Also, for some reason I had to change AndroidHttpClient to DefaultHttpClient in HttpUtils) – njzk2 Nov 22 '11 at 16:40
  • 1
    Please provide more clearity on implementation, I want to send MMS programmatically in my project.But Unable to use your shown code.. – Arpit Garg Jan 18 '12 at 14:17
  • is this the only way i can send MMS?also is this a complete code?Thanks – Maha Mar 29 '12 at 20:13
  • I am receiving this error: java.lang.SecurityException: No permission to write APN settings: Neither user 10099 nor current process has android.permission.WRITE_APN_SETTINGS. The error gets thrown after querying the Carriers ContentProvider in the getMMSApns() method of the APNHelper class. final Cursor apnCursor = this.context.getContentResolver().query(Uri.withAppendedPath(Carriers.CONTENT_URI, "current"), null, null, null, null); I am testing on a Nexus 4, its on Android v4.2.2. Is anyone else getting this error? – Etienne Lawlor Mar 09 '13 at 08:41
  • final int result = connMgr.startUsingNetworkFeature( ConnectivityManager.TYPE_MOBILE, Phone.FEATURE_ENABLE_MMS); This doesn't active the APN for the first time.. it return value of 2. once I send MMS from the default messagner then the above code seems to be work.. please can you suggest me the right approach.. – kamal_tech_view Mar 29 '13 at 08:58
  • One of the problems I ran into concerned "security" on the part of the carrier. I am unable to lookup the DNS for the MMSC unless I am connected to the mobile network - it doesn't work on the Wifi - I found code with a function "forceMobileConnectionForAddress" that worked great. – JavaCoderEx Jun 10 '13 at 02:54
  • 1
    this site explains it much better: http://forum.xda-developers.com/showthread.php?t=2222703 – Sam Adamsh Jun 23 '13 at 21:08
  • @SamAdams I've implemented this code, but getting connection time out for the HTTP request, I've tried with different networks also but no change, Can you post the working code. – Jaldip Katre Dec 10 '13 at 05:51
  • Dear Damian and @njzk2 Can you please explain it how can I use this code? I'm beginner here, Please help me. – Nauman Zubair Jan 03 '15 at 18:50
  • cant we use this methods `SmsManager.sendMultimediaMessage (Context context, Uri contentUri, String locationUrl, Bundle configOverrides, PendingIntent sentIntent)` describe in android site to send mms. if we can then any how tell send from sim 1 or 2?[https://developer.android.com/reference/android/telephony/SmsManager.html#sendMultimediaMessage(android.content.Context,%20android.net.Uri,%20java.lang.String,%20android.os.Bundle,%20android.app.PendingIntent)] – Sohail Zahid May 26 '16 at 07:32
9

If you have to send MMS with any Image using Intent then use this code.

Intent sendIntent = new Intent(Intent.ACTION_SEND); 
sendIntent.setClassName("com.android.mms", "com.android.mms.ui.ComposeMessageActivity");
sendIntent.putExtra("sms_body", "some text"); 
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/image_4.png"));
sendIntent.setType("image/png");
startActivity(sendIntent);;

OR

If you have to send MMS with Audio or Video file using Intent then use this.

Intent sendIntent = new Intent(Intent.ACTION_SEND); 
sendIntent.setClassName("com.android.mms", "com.android.mms.ui.ComposeMessageActivity");
sendIntent.putExtra("address", "1213123123");
sendIntent.putExtra("sms_body", "if you are sending text");   
final File file1 = new File(mFileName);
if(file1.exists()){
  System.out.println("file is exist");
}
Uri uri = Uri.fromFile(file1);
sendIntent.putExtra(Intent.EXTRA_STREAM, uri);
sendIntent.setType("video/*");
startActivity(sendIntent);

let me know if this help you.

Etienne Lawlor
  • 6,817
  • 18
  • 77
  • 89
swiftBoy
  • 35,607
  • 26
  • 136
  • 135
  • 4
    This invokes the native Messaging Application. But is there a way to send MMS within your own application and register listening for incoming **MMS** messages with a `BroadcastReceiver` in a similar fashion to how this is implemented for **SMS** messages. – Etienne Lawlor Jan 22 '13 at 05:28
  • I find this to work properly with images, but not audio/video (your second code example). – dadude999 May 22 '15 at 04:17
9

This seems to be answered in the post: Sending MMS with Android

Key lines of code being:

Intent sendIntent = new Intent(Intent.ACTION_SEND); 
sendIntent.putExtra("sms_body", "some text"); 
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(url));
sendIntent.setType("image/png"); 
aioobe
  • 413,195
  • 112
  • 811
  • 826
  • 2
    something to keep in mind is the image should be on external storage or as a content provide. normally internal apps data can't be accessed by other apps. so you have to temporarily write image to external storage and then pass the path to `Uri.parse` – Shubhank May 08 '13 at 13:18
  • this doesn't work anymore since choosing the message activity in the phone makes the message contain only the picture, without the text – peresisUser Jan 31 '18 at 13:31
  • @peresisUser Any thoughts or code snippets on what does work currently? – AJW Apr 22 '20 at 14:31
1

The answer with the APN helper will not work after android 4.0. To get mms apn settings on Android 4.0 and above view this answer: View mms apn

Community
  • 1
  • 1
Sam Adamsh
  • 3,331
  • 8
  • 32
  • 53