Home > front end >  How do we attach multiple images on intent in android email
How do we attach multiple images on intent in android email

Time:03-22

I'm struggling with attaching more than one image before before sending it to gmail or other email client. I would like to have each button attach an image, for example I would like user to attach on first button, copy of ID then another button, proof of payment copy(image).

My java code

 private void openFolder() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    intent.putExtra("return-data", true);
    startActivityForResult(Intent.createChooser(intent, "Complete action using"), PICK_FROM_GALLERY);
}

private void openFolder2() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    intent.putExtra("return-data", true);
    startActivityForResult(Intent.createChooser(intent, "Complete action using"), PICK_FROM_GALLERY2);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == PICK_FROM_GALLERY && resultCode == RESULT_OK)
     {
         URI = data.getData();
         tvAttachment.setText(URI.getLastPathSegment());
         tvAttachment.setVisibility(View.INVISIBLE);

         URI2 = data.getData();
         tvAttachment2.setText(URI.getLastPathSegment());
         tvAttachment2.setVisibility(View.INVISIBLE);

    }
}

private void sendEmail() {
    try {
        String recipient = "[email protected]";
        subject = etSubject.getText().toString();
        message = "Full Name: "   Name.getText().toString()   "\n"   "Cellphone No: "   Number.getText().toString();
        final Intent emailIntent = new Intent(Intent.ACTION_SEND);
        emailIntent.setType("plain/text");
        emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{recipient});
        emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
        if (URI != null) {
            emailIntent.putExtra(Intent.EXTRA_STREAM, URI);
        }
        emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, message);
        this.startActivity(Intent.createChooser(emailIntent, "Sending email..."));
    } catch (Throwable t) {
        Toast.makeText(this, "Request failed, retry! "   t.toString(), Toast.LENGTH_LONG).show();
    }
}

CodePudding user response:

Change your sendEmail function to the following:

private void sendEmail() {
    try {
        String recipient = "[email protected]";
        subject = etSubject.getText().toString();
        message = "Full Name: "   Name.getText().toString()   "\n"   "Cellphone No: "   Number.getText().toString();
        final Intent emailIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
        emailIntent.setType("*/*");
        emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{recipient});
        emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
        ArrayList<Uri> uris = new ArrayList<Uri>();
        uris.add(URI);
        uris.add(URI2);
        emailIntent.putExtra(Intent.EXTRA_STREAM, uris);
        emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, message);
        this.startActivity(Intent.createChooser(emailIntent, "Sending email..."));
    } catch (Throwable t) {
        Toast.makeText(this, "Request failed, retry! "   t.toString(), Toast.LENGTH_LONG).show();
    }
}

As someone in comments pointed out, your MIME type is invalid (reversed.) Safest bet is to use "*/*". You also need to use ACTION_SEND_MULTIPLE for the Intent action since you are attaching multiple Uris.

  • Related