Send an MMS using B4A - question

Discussion in 'Android Questions' started by NJDude, Mar 27, 2012.

  1. NJDude

    NJDude Expert Licensed User

    How do you do something like this on B4A (The forum search is unavailable at this moment)
    Code:
    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.
     
  2. thedesolatesoul

    thedesolatesoul Expert Licensed User

    Maybe try this as a string:
    "android.intent.extra.STREAM"
     
  3. Erel

    Erel Administrator Staff Member Licensed User

    That is correct. Intent.EXTRA_STREAM is a string constant.
     
  4. NJDude

    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.
    Code:
    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
     
  5. thedesolatesoul

    thedesolatesoul Expert Licensed User

    Do you want to send the mms?

    Then:
    Code:
    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?
     
  6. NJDude

    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.
     
  7. Erel

    Erel Administrator Staff Member Licensed User

    Try this:
    Code:
    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 StringAs Object
       
    Dim r As Reflector
       
    Return r.RunStaticMethod("android.net.Uri""parse"Array As Object(uri), Array As String("java.lang.String"))
    End Sub
     
    Steini1980 likes this.
  8. NJDude

    NJDude Expert Licensed User

    I ran the code and I get this error:
    Code:
    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
     
  9. Erel

    Erel Administrator Staff Member Licensed User

    Change iIntent.EXTRA_STREAM to "android.intent.extra.STREAM".

    I don't think that this intent will work with a file from the assets folder. The target application will not have access to this folder.

    You need to use the storage card.
     
  10. NJDude

    NJDude Expert Licensed User

    This code seems to work, but I don't see the attachment:
    Code:
    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)
     
  11. Erel

    Erel Administrator Staff Member Licensed User

    Your intent doesn't look like the one in the first post.
     
  12. NJDude

    NJDude Expert Licensed User

    Eureka!!!

    Ok, the code below is able to send an MMS attaching a file, in this case, a picture.
    Code:
    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 StringAs 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: Mar 28, 2012
  13. NeoTechni

    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.

    Code:
    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 StringAs Object
        
    Dim r As Reflector
        
    Return r.RunStaticMethod("android.net.Uri""parse"Array As Object(uri), Array As String("java.lang.String"))          
    End Sub
     
    Jehoschua and luke2012 like this.
  14. NJDude

    NJDude Expert Licensed User

    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.
     
    Jehoschua likes this.
  15. beni_feld

    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
     
  16. Erel

    Erel Administrator Staff Member Licensed User

    This means that there is no app installed on your device that can handle such intent.
     
  17. ThRuST

    ThRuST Well-Known Member Licensed User

    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.
     
  18. NJDude

    NJDude Expert Licensed User

    Add this line to that code, it should work:
    Code:
    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: Dec 4, 2013
  19. Erel

    Erel Administrator Staff Member Licensed User

    You can check with PackageManager.QueryIntentActivities whether there is any installed app that can handle the intent. You can also add a Try / Catch block to catch this error.
     
    luke2012 likes this.
  20. ThRuST

    ThRuST Well-Known Member Licensed User

    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?
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice