How to send image via MMS in Android?
Asked Answered
J

4

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.

Juniorjuniority answered 4/6, 2010 at 9:23 Comment(1)
I am doing something similar HERE!!! #14453308Cacciatore
T
41

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;

}
Tweeze answered 4/6, 2010 at 9:51 Comment(12)
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.Trigger
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.Gow
totally works! much kudos! (Also, for some reason I had to change AndroidHttpClient to DefaultHttpClient in HttpUtils)Denominator
Please provide more clearity on implementation, I want to send MMS programmatically in my project.But Unable to use your shown code..Omor
is this the only way i can send MMS?also is this a complete code?ThanksSogdiana
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?Cacciatore
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..Stanger
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.Setsukosett
this site explains it much better: forum.xda-developers.com/showthread.php?t=2222703Prophet
@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.Ferminafermion
Dear Damian and @Denominator Can you please explain it how can I use this code? I'm beginner here, Please help me.Gantlet
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?[developer.android.com/reference/android/telephony/…Dr
F
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"); 
Frightened answered 4/6, 2010 at 9:29 Comment(3)
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.parseMoonwort
this doesn't work anymore since choosing the message activity in the phone makes the message contain only the picture, without the textBuiron
@Buiron Any thoughts or code snippets on what does work currently?Keeter
J
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.

Jennelljenner answered 15/1, 2013 at 11:51 Comment(2)
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.Cacciatore
I find this to work properly with images, but not audio/video (your second code example).Ribband
P
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

Prophet answered 19/7, 2013 at 20:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.