Search code examples
androidandroid-intentmessagemms

Android MMS Intent with with Video file and body text


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"));

this code send mms with image and text.

But how send video file instead of image ?


Solution

  • I assume you want to pick your video from your memory. Then, firstly you have to import:

    import android.app.Activity;
    import android.content.Intent;
    import android.database.Cursor;
    import android.net.Uri;
    import android.os.Bundle;
    import android.provider.MediaStore;
    import android.view.View;
    import android.view.Window;
    import android.view.WindowManager;
    import java.io.File;
    

    This is how activity looks like when you want to import video from Gallery to MMS:

    public class MMS extends Activity {
    
    // Fields
    private String TAG = "MMS";
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    
    // To display current window in full screen.
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
    WindowManager.LayoutParams.FLAG_FULLSCREEN);
    
    /**
    * See assets/res/layout/mms.xml for this view layout
        * definition, which is being set here as the content of our screen.
    */
    
    setContentView(R.layout.mms);
    
    }
    
    /*
     * Open Gallery to select video to send on OnClick Event of someButton.
     */
    
    public void onClickPicMMS(View view) {
    Intent intent = new Intent();
    intent.setType("video/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Video"),0);
    
    }
    
    
    /*
     * Called when an activity you launched exits, giving you the requestCode you started it with, the resultCode it returned, and any additional data from it.
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    
        if (requestCode == 0 && resultCode == RESULT_OK) {
    
            // Fetch the path of selected image.
            Uri uri = data.getData();
            String[] projection = { MediaStore.Video.Media.DATA };
            Cursor cursor = managedQuery(uri, projection, null, null, null);
            int column_index = cursor
                    .getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
            cursor.moveToFirst();
            String mmsvideoPath = cursor.getString(column_index);
    
            //Call the intent to send selected video as MMS.
            Intent intent = new Intent(Intent.ACTION_SEND);
            File f = new File(mmsvideoPath);
            intent.putExtra("sms_body", "Hi how are you");
            intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f)); // videoUri set previously
            sendIntent.setType("video/3gp");
            startActivity(intent);
    
        }
    }
    }
    

    Insert the permission in Manifest:

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET" />