Send an MMS using B4A - question

NJDude

Expert
Licensed User
Longtime User
How do you do something like this on B4A (The forum search is unavailable at this moment)
B4X:
Uri mmsUri = Uri.parse("content://media/external/images/media/1"); 
Intent intent = new Intent(Intent.ACTION_SEND); 
intent.putExtra("sms_body", "Hi how are you");
intent.putExtra(Intent.EXTRA_STREAM, mmsUri); 
intent.setType("image/png"); 
startActivity(intent);

I can's see how to add EXTRA_STREAM.

Thanks.
 

ThRuST

Well-Known Member
Licensed User
Longtime User
Declaring the activity in the manifest

You must declare your activity in the manifest file in order for it to be accessible to the system. To declare your activity, open your manifest file and add an <activity> element as a child of the <application> element. For example:

<manifest ... >
<application ... >
<activityandroid:name=".ExampleActivity"/>
...
</application ... >
...</manifest>


Anyone knows how this is done?
 
Upvote 0

air cover

Member
Licensed User
Longtime User
Am I missing a trick? The code for this thread pops up the Android text function. You can manually hit its SEND button, but only the text transmits...not the attachment.
 
Upvote 0

air cover

Member
Licensed User
Longtime User
OK, fairly subtle change, but this works on my Samsung Infusion test phone (changing the Initialize line to "android.intent.action.SEND" instead of SEND_MSG):

B4X:
Sub SendPhotoMessage(PhoneNumber As String, Message As String, Dir As String, Filename As String)
  Dim iIntent As Intent

  iIntent.Initialize("android.intent.action.SEND", "")
  iIntent.setType("vnd.android-dir/mms-sms")
  iIntent.PutExtra("android.intent.extra.STREAM", CreateUri("file://" & File.Combine(Dir, Filename)))
  iIntent.PutExtra("sms_body", Message)
  iIntent.PutExtra("address", PhoneNumber)
  iIntent.SetType("image/png")
  iIntent.SetComponent("com.android.mms/.ui.ComposeMessageActivity")
  StartActivity(iIntent)

End Sub

With the above change/code, my app transmits pictures via MMS now. Tres Coolio.
 
Upvote 0

air cover

Member
Licensed User
Longtime User
I also added these two lines to the bottom of my Manifest file:

AddPermission("android.permission.SEND_SMS")
AddPermission("android.permission.SEND_MMS")
 
Upvote 0

air cover

Member
Licensed User
Longtime User
So, it works...but the user must still manually press the SEND key when the stock Android MMS app appears, and after the SEND is pressed the default MMS app remains on screen instead of returning to my own App that called it.

Any tips from you pros would be appreciated!
 
Upvote 0

air cover

Member
Licensed User
Longtime User
Declaring the activity in the manifest

You must declare your activity in the manifest file in order for it to be accessible to the system. To declare your activity, open your manifest file and add an <activity> element as a child of the <application> element. For example:

<manifest ... >
<application ... >
<activityandroid:name=".ExampleActivity"/>
...
</application ... >
...</manifest>


Anyone knows how this is done?

I would guess that in the B4A IDE you would go to Project --> Manifest Editor

...and then on a new bottom line in the manifest editor type something like:

B4X:
AddActivityText(Main,
<activity
android:name="com.android.mms.ui.ComposeMessageActivity"
android:launchMode="singleTop">

<intent-filter>
  <action android:name="android.intent.action.SEND" />
  <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

<intent-filter>
  <action android:name="com.android.mms/.ui.ComposeMessageActivity" />
</intent-filter>

</activity>)
 
Last edited:
Upvote 0

air cover

Member
Licensed User
Longtime User
OK, the above post works for Android 2.x. For Android 4.x (also works for 2.x), you've got to go a slightly different route:

B4A code
B4X:
            iIntent.Initialize("android.intent.action.SEND", "")
            iIntent.setType("vnd.android-dir/mms-sms")
            iIntent.PutExtra("android.intent.extra.STREAM", CreateUri_Click("file://" & File.Combine(Dir, Filename)))
            iIntent.PutExtra("sms_body", Message)
            iIntent.PutExtra("address", PhoneNumber)
            iIntent.SetType("image/png")
            'iIntent.SetComponent("com.android.mms/.ui.ComposeMessageActivity")
            iIntent.SetComponent(".ComposeSmsActivity")
            StartActivity(iIntent)


AndroidManifest.xml code:
B4X:
AddActivityText(Main,
        <!-- Activity that allows the user to send new SMS/MMS messages -->
        <activity android:name=".ComposeSmsActivity" >
            <intent-filter>
                <action android:name="android.intent.action.SEND" />              
                <action android:name="android.intent.action.SENDTO" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="sms" />
                <data android:scheme="smsto" />
                <data android:scheme="mms" />
                <data android:scheme="mmsto" />
            </intent-filter>
        </activity>)
 
Last edited:
Upvote 0

luke2012

Well-Known Member
Licensed User
Longtime User
Eureka!!!

Ok, the code below is able to send an MMS attaching a file, in this case, a picture.
B4X:
Dim iIntent As Intent

iIntent.Initialize("android.intent.action.SEND_MSG", "")

iIntent.setType("vnd.android-dir/mms-sms")
iIntent.PutExtra("android.intent.extra.STREAM", CreateUri("file://" & File.Combine(File.DirDefaultExternal, "exiticon.png")))
iIntent.PutExtra("sms_body", "Hello there!!!")
iIntent.PutExtra("address", "1234")
iIntent.SetType("image/png")

StartActivity(iIntent)

-------------------------------------------

Sub CreateUri(uri As String) As Object
   
    Dim r As Reflector

    Return r.RunStaticMethod("android.net.Uri", "parse", Array As Object(uri), Array As String("java.lang.String"))
           
End Sub

It took a little bit of research and tweaking, Erel, thanks for that clue.

Thanks

@NJDude & @Erel I'm trying to do the same thing but using whatsapp.

How can I arrange the code using WhatsApp as target App to send the message?
 
Upvote 0

eleandrot

Member
Licensed User
Longtime User
No meu funciona tudo
porém tem que pressionar SEND ou seja
ele NÃO AUTOMATIZA..
como fazer para automatizar ?
 
Upvote 0

eleandrot

Member
Licensed User
Longtime User
Por favor me ajudem, ja consigo colocar na caixa de saída mas quero AUTOMATIZAR, não quero pressionar SEND
 
Upvote 0

eleandrot

Member
Licensed User
Longtime User
thank you, sms is simple, my problem is to automate the sending of MMS without intent to use, or make a program for sending mms
 
Upvote 0
Top