1
private void sendPhotoByEmail(Uri photoUri, String[] emailTo) {
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
        intent.setType("message/rfc822");
        intent.putExtra(android.content.Intent.EXTRA_EMAIL, emailTo);
        String intro;
        if (mData != null) {
            intro = mData.optString(DecisionTree.EMAIL_BODY, getString(R.string.share_photo_email_body_format));
        } else {
            intro = getString(R.string.share_photo_email_body_format);
        }
        intro = String.format("%s\n\n%s", intro, getString(R.string.share_photo_email_body_footer,
                                                           ApplicationContext.getFormattedVersionInfo(this)));
        String locationText = intro + ApplicationContext.getUserLocationBody();
        intent.putExtra(android.content.Intent.EXTRA_SUBJECT, getString(R.string.share_photo_subject));
        intent.putExtra(android.content.Intent.EXTRA_TEXT, locationText);
        intent.putExtra(Intent.EXTRA_STREAM, photoUri);
        startActivity(intent);
    }

    private void sendPhotoByText(Uri photoUri, String phoneTo) {
        Toast.makeText(this, "In sendPhotoByText", Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
        intent.setType("image/*");
        intent.setData(Uri.parse("smsto:" + phoneTo));
        String intro;
        if (mData != null) {
            intro = mData.optString(DecisionTree.EMAIL_BODY, getString(R.string.share_photo_email_body_format));
        } else {
            intro = getString(R.string.share_photo_email_body_format);
        }
        intro = String.format("%s\n\n%s", intro, getString(R.string.share_photo_email_body_footer,
                                                           ApplicationContext.getFormattedVersionInfo(this)));
        String locationText = intro + ApplicationContext.getUserLocationBody();
        intent.putExtra("sms_body", locationText);
        intent.putExtra(Intent.EXTRA_STREAM, photoUri);
        startActivity(intent);
    }

In my app, the user chooses a photo from the Gallery. It is saved and the uri is passed to one of the two methods above. The sendPhotoByEmail method formats an email with the photo in it. The sendPhotoByText method formats a text message. The photo does not appear in it, and I see the error message "Unable to attach. File not supported". I have tested with the same photo (same uri) in each function. The uri is "content://media/external/images/media/1017". I have also run Messenger separately and was able to attach the same photo successfully.

I have set the SMS and Storage permissions in the app on my device, and these are the permissions in AndroidManifest.xml:

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.WRITE_CONTACTS" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-permission android:name="android.permission.SEND_SMS />

Any insights would be greatly appreciated.

Believe it or not, I replaced

        intent.setData(Uri.parse("smsto:" + phoneTo));

with

        intent.putExtra("address", phoneTo);

and the photo appeared in the formatted text message. However, the screen that shows apps to choose from (which previously showed only text messaging apps) now shows other apps as well. I would like it to show only text messaging apps.

scrayne
  • 701
  • 7
  • 18
  • Not sure what the issue is but just a heads up you don't need the SMS permission to use intents – Nick Mowen Oct 11 '19 at 05:25
  • 1
    "The photo does not appear in it, and I see the error message "Unable to attach. File not supported"." -- add `Intent.FLAG_GRANT_READ_URI_PERMISSION` to the `Intent`, along with whatever other flags you wish. Without that, the other app has no rights to read the content identified by your `Uri`. – CommonsWare Oct 11 '19 at 12:55
  • If you read my comment, you will see that I solved the "no photo" problem, and now my issue is that when a panel of apps is shown for the user to choose from, several apps that are not sms/mms apps are shown. I would like for only sms/mms apps to be shown in that panel – scrayne Oct 14 '19 at 01:53

1 Answers1

0

Based on your comments above, seems like you'd like to only have your intent shared with SMS/MMS apps. Have you tried setting the intent type with intent.setType("vnd.android-dir/mms-sms")?

Michael Evans
  • 495
  • 3
  • 6
  • Unfortunately that didn't work. In fact it did the opposite of what I wanted -- it displayed all the apps I might want to use except the SMS/MMS apps. – scrayne Oct 18 '19 at 14:10