Send an MMS using B4A - question

NJDude

Expert
Licensed 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.
 

NJDude

Expert
Licensed User
The problem I'm having is how to translate the above code into B4A, I've tried the one below but is not working.
B4X:
Dim iIntent As Intent 

iIntent.Initialize(iIntent.ACTION_VIEW, "mms:")
iIntent.PutExtra(iIntent.EXTRA_STREAM, FileNane)  '<-- What to do here?
iIntent.setType("image/png") 

StartActivity(iIntent)
I guess I'm having a bad day, doing something wrong or missing something :D
 

thedesolatesoul

Expert
Licensed User
Do you want to send the mms?

Then:
B4X:
Dim iIntent As Intent 

iIntent.Initialize(iIntent.ACTION_SEND, "mms:")
iIntent.PutExtra("android.intent.extra.STREAM", FileNane)  '<-- What to do here?
iIntent.setType("image/png") 

StartActivity(iIntent)
What content do you want to send? Is it a file? You might need an inputstream there?
 

NJDude

Expert
Licensed User
Yes, I'd like to attach a picture to the MMS, so, using the intent, I open the MMS client with the message and file already attached ready to send.
 

Erel

Administrator
Staff member
Licensed User
Try this:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   iIntent.PutExtra(iIntent.EXTRA_STREAM, CreateUri("file://" & File.Combine(File.DirRootExternal, "1.jpg")))
End Sub

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
 

NJDude

Expert
Licensed User
I ran the code and I get this error:
B4X:
Compiling code.                         Error
Error compiling program.
Error description: Unknown member: extra_stream
Occurred on line: 132
iIntent.PutExtra(iIntent.EXTRA_STREAM, CreateUri("file://" & File.Combine(File.DirAssets, "exiticon.jpg")))
Word: extra_stream
 

NJDude

Expert
Licensed User
This code seems to work, but I don't see the attachment:
B4X:
Dim iIntent As Intent 

iIntent.Initialize(iIntent.ACTION_VIEW, "mms:")
iIntent.PutExtra("android.intent.extra.STREAM", CreateUri("file://" & File.Combine(File.DirDefaultExternal, "exiticon.png")))
iIntent.PutExtra("sms_body", "Hello")

StartActivity(iIntent)
 

NJDude

Expert
Licensed 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
 
Last edited:

NeoTechni

Well-Known Member
Licensed User
I tweaked your code to be re-usable. I mean no offense, but you should always write your code this way from the start.

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_MSG", "")
   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")
   StartActivity(iIntent)
End Sub
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
 

NJDude

Expert
Licensed User
I tweaked your code to be re-usable. I mean no offense, but you should always write your code this way from the start.
Non taken, well, one of the beauties of posting code is that members can use it or modify it as they please, I just posted my solution and you improved it.
 

beni_feld

Member
Licensed User
I tried the sample code and got the following error message:
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.SEND_MSG typ=image/png flg=0x20000 (has extras) }

Please help
 

ThRuST

Well-Known Member
Licensed User
I tried the sample code and got the following error message:
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.SEND_MSG typ=image/png flg=0x20000 (has extras) }

Please help
So are you satisfied with this as a solution?? or can anyone provide a workaround? I posted a private conversation about this, but Erel prefer to answer public threads that's why I post my question here. The 'solution' so far seems very unstable. That it might work for someone is not the answer I am looking for. I hope someone can provide a professional (stable) solution of this. It will be of great help to everyone. Thanks everyone.
 

NJDude

Expert
Licensed User
Add this line to that code, it should work:
B4X:
iIntent.SetComponent("com.android.mms/.ui.ComposeMessageActivity")
As mentioned by Erel above, the device should be able to send SMS messages, WiFi only devices can't do that.
 
Last edited:

ThRuST

Well-Known Member
Licensed User
Add this line to that code, it should work:
B4X:
iIntent.SetComponent("com.android.mms/.ui.ComposeMessageActivity")
As mentioned by Erel above, the device should be able to send SMS messages, WiFi only devices can't do that.
I added your line to the above code which generated the following error message:

Error occured

An error has occured in sub: main_vvvvv0(java line: 1342) android.content.
ActivityNotFoundException:
Unable to find explicity activity class {com.android.mms/com.android.mms.ui.
ComposeMessageActivity}; have you declared this activity in your AndroidManifest.xml?
Continue?


How can this be declared this in the manifest file?
 
Top