It looks like that ACTION_SENDTO doesn't support attachments officially. It works only for Gmail and default email client because they use "undocumented" feature. The only way I found to send emails with attachments is to use ACTION_SEND instead.
My solution:
public class EmailSender {
private static final String INTENT_DATA_SCHEME_EMAIL = "mailto:";
private static final String FILE_PROVIDER_AUTHORIZER = "com.smartinhalerlive.app.fileprovider";
private String subject;
private String body;
private File attachment;
public EmailSender(String subject, String body, File attachment) {
this.subject = subject;
this.body = body;
this.attachment = attachment;
}
public void sendEmail(Context context, EmailSenderListener listener) {
List<ResolveInfo> emailClients = getEmailClients(context);
if (null != emailClients) {
ResolveInfo defaultEmailClient = getDefaultEmailClient(emailClients);
if (null != defaultEmailClient) {
sendEmailUsingSelectedEmailClient(context, defaultEmailClient, listener);
} else {
showSelectEmailClientsDialog(context, emailClients, listener);
}
}
}
private void showSelectEmailClientsDialog(Context context,
List<ResolveInfo> emailClients,
EmailSenderListener listener) {
String[] emailClientNames = getEmailClientNames(context, emailClients);
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(R.string.email_dialog_title);
builder.setItems(emailClientNames, (dialog, which) -> {
sendEmailUsingSelectedEmailClient(context, emailClients.get(which), listener);
}
);
builder.create().show();
}
@NonNull
private static String[] getEmailClientNames(Context context, List<ResolveInfo> emailClients) {
PackageManager packageManager = context.getPackageManager();
String[] emailClientNames = new String[emailClients.size()];
for (int i = 0; i < emailClients.size(); i++) {
emailClientNames[i] = emailClients.get(i).activityInfo.applicationInfo.loadLabel(packageManager).toString();
}
return emailClientNames;
}
@Nullable
private static ResolveInfo getDefaultEmailClient(List<ResolveInfo> emailClients) {
ResolveInfo defaultEmailClient = null;
if (emailClients.size() == 1) {
defaultEmailClient = emailClients.get(0);
} else {
for (ResolveInfo emailClient : emailClients) {
if (emailClient.isDefault) {
defaultEmailClient = emailClient;
break;
}
}
}
return defaultEmailClient;
}
private static List<ResolveInfo> getEmailClients(Context context) {
PackageManager pm = context.getPackageManager();
Intent emailDummyIntent = getEmptyEmailIntent();
return pm.queryIntentActivities(emailDummyIntent, 0);
}
public static Intent getEmptyEmailIntent() {
Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
emailIntent.setData(Uri.parse(INTENT_DATA_SCHEME_EMAIL));
return emailIntent;
}
private static void grantReadPermissionForAttachmentUri(Context context,
Intent emailIntent,
Uri attachmentUri) {
if (emailIntent == null || attachmentUri == null) {
return;
}
context.grantUriPermission(emailIntent.getComponent().getPackageName(),
attachmentUri,
Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
private void sendEmailUsingSelectedEmailClient(Context context,
ResolveInfo emailClient,
EmailSenderListener listener) {
try {
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, null != subject ? subject : "");
emailIntent.putExtra(Intent.EXTRA_TEXT, null != body ? body : "");
emailIntent.setComponent(new ComponentName(emailClient.activityInfo.packageName, emailClient.activityInfo.name));
addAttachment(context, attachment, emailIntent);
listener.onEmailIntentReady(emailIntent);
} catch (Exception ex) {
Timber.e("Error sending email", ex);
}
}
private static void addAttachment(Context context, File attachment, Intent emailIntent) {
if (attachment != null) {
Uri contentUri = FileProvider.getUriForFile(context, FILE_PROVIDER_AUTHORIZER, attachment);
grantReadPermissionForAttachmentUri(context, emailIntent, contentUri);
emailIntent.putExtra(Intent.EXTRA_STREAM, contentUri);
}
}
public interface EmailSenderListener {
void onEmailIntentReady(Intent emailIntent);
}
}