user432209
user432209

Reputation: 20167

Using an MMS intent with an image file

So I am trying to invoke an intent that will attach a picture to a text message. The following code correctly brings up a text message window with the image on the emulator, but crashed on my phone (Droid X).

            String name = FILE_NAME;

            Uri uri = Uri.fromFile(new File(name));

            Intent intent = new Intent(Intent.ACTION_SEND); 
            intent.setClassName("com.android.mms", "com.android.mms.ui.ComposeMessageActivity"); 
            intent.putExtra(Intent.EXTRA_STREAM, uri);
            intent.setType("image/png"); 
            startActivity(intent); 

.

12-30 12:56:50.628: ERROR/AndroidRuntime(14603): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.android.mms/com.android.mms.ui.ComposeMessageActivity}; have you declared this activity in your AndroidManifest.xml?

The problem is the .setClassName line. I hardcoded the value to the class, which is probably wrong. What should I be using here to support all phones?

Thanks all.

Upvotes: 0

Views: 1667

Answers (2)

sendIntent.setPackage("com.android.mms");

that will solve your issue

Upvotes: 0

Ginger McMurray
Ginger McMurray

Reputation: 1147

Do not set the class name at all. This will cause Android to find the best possible activity for you, launching the app chooser dialog if necessary.

An alternative would be to resolve all possible activities for the intent and use some logic to determine which one you prefer, and default to letting Android choose if your logic can't pick one. That's a little over the top though, and if your goal is just to let the user send an MMS there's no need for that level of detail.

Upvotes: 3

Related Questions