random.123
random.123

Reputation: 65

Sending data with multiple attachments with email intent

I have a FeedbackActivity.java activity which takes feedback from user with multiple attachments (upto 3 images as attachments).

I am using following code:

Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);     
intent.setType("message/rfc822");
intent.putExtra(Intent.EXTRA_EMAIL, emails);                   //emails is an Array of 'String' type
intent.putExtra(Intent.EXTRA_SUBJECT, subject);                //subject is a String
intent.putExtra(Intent.EXTRA_TEXT, text)                       //text is a String
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris); //uris is an ArrayList of 'Uri' type
                                                              //uris stores all Uri of images selected

if(intent.resolveActivity(getPackageManager()) != null){
    startActivity(intent);
}
else {
    Toast.makeText(this, "Not Good", Toast.LENGTH_SHORT).show();
}

Now this code works fine but the problem is that it shows all sorts of apps which support "message/rfc822" MIME type.

Image is shown below :

enter image description here

I only need to show the email client apps, I tried Uri.parse("mailto:"), but didn't workout and code always moves to else statement and shows the toast "not good".

I read the google documentation but it only shows simple cases. I tried searching on the web. Many developers are using intent.setType("*/*") or intent.setType("text/plain"). But they all too show apps other than email clients.

Please guide me.

And I wanted to ask in general,

Google documentations show simple examples which is good in a way, but how to learn really in depth on these kind of topics?

Thank you.

Upvotes: 0

Views: 218

Answers (2)

random.123
random.123

Reputation: 65

So here, we will be using two intents: selectorIntent and emailIntent. selectorIntent is what the emailIntent will use as to show available apps. code:

Intent selectorIntent = new Intent(Intent.ACTION_SENDTO);
selectorIntent.setData(Uri.parse("mailto:"));

final Intent emailIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
emailIntent.putExtra(Intent.EXTRA_EMAIL, emails);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
emailIntent.setSelector(selectorIntent);
if(emailIntent.resolveActivity(getPackageManager()) != null){
    startActivity(emailIntent);
}
else {
    Snackbar.make(scrollView, "Sorry, We couldn't find any email client apps!", Snackbar.LENGTH_SHORT).show();
}

Now it will choose only apps which are email client.

If there is only one email-client app in your phone than it will directly open that. And if no such application is there, than the code will show Snackbar given in the else part.

Upvotes: 3

gcantoni
gcantoni

Reputation: 787

Don't use Uri.parse, use Uri.fromParts

Do it like this:

Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto","[email protected]", null));

Upvotes: -1

Related Questions