There are two requirements:
- Email with attachment
- In the
Intent
chooser, there should be only Email apps.
What I have known/done:
Intent.ACTION_SENDTO
withintent.setData(Uri.parse("mailto:"))
can make sure that there are only Email apps inIntent
chooser but it will not bring attachment(For some apps like Gmail it will, but there are also many apps that will ignore attachment).Intent.ACTION_SEND
can send Email with attachment. However, inIntent
chooser, there will be apps that are actually not Email apps but can response toIntent.ACTION_SEND
. Usingintent.setType("message/rfc822")
can reduce number of those apps but not all.References this answer: https://stackoverflow.com/a/8550043/3952691 and nearly succeed in my goals. My code is as below:
private static void sendFeedbackWithAttachment(Context context, String subject) { Intent intent = new Intent(Intent.ACTION_SENDTO); intent.setData(Uri.parse("mailto:")); PackageManager packageManager = context.getPackageManager(); List<ResolveInfo> resolveInfos = packageManager.queryIntentActivities(intent, 0); if (resolveInfos.isEmpty()) { Toast.makeText(context, context.getString(R.string.error_activity_not_found), Toast.LENGTH_SHORT).show(); } else { // ACTION_SEND may be replied by some apps that are not email apps. However, // ACTION_SENDTO doesn't allow us to choose attachment. As a result, we use // an ACTION_SENDTO intent with email data to filter email apps and then send // email with attachment by ACTION_SEND. List<LabeledIntent> intents = new ArrayList<>(); Uri uri = getLatestLogUri(); for (ResolveInfo info : resolveInfos) { Intent i = new Intent(Intent.ACTION_SEND); i.setPackage(info.activityInfo.packageName); i.setClassName(info.activityInfo.packageName, info.activityInfo.name); i.putExtra(Intent.EXTRA_EMAIL, new String[] { Def.Meta.FEEDBACK_EMAIL }); i.putExtra(Intent.EXTRA_SUBJECT, subject); i.putExtra(Intent.EXTRA_STREAM, uri); intents.add(new LabeledIntent(i, info.activityInfo.packageName, info.loadLabel(context.getPackageManager()), info.icon)); } Intent chooser = Intent.createChooser(intents.remove(0), context.getString(R.string.send_feedback_to_developer)); chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, intents.toArray(new LabeledIntent[intents.size()])); context.startActivity(chooser); } }
However, on some devices(For example, Xiaomi 2S with MIUI V5, I don't know if this can be influenced by a third-party rom), the result is an empty
Intent
chooser. And it seems that above Android 6.0,Intent.EXTRA_INITIAL_INTENTS
has some bugs(Custom intent-chooser - why on Android 6 does it show empty cells?, and another one: https://code.google.com/p/android/issues/detail?id=202693).
As a result, I don't know how to achieve my goals. Please help me, thank you in advance.