我们如何在 android 电子邮件中按意图附加多张图片

How do we attach multiple images on intent in android email

在将图像发送到 gmail 或其他电子邮件客户端之前,我很难附加一张以上的图像。我想让每个按钮都附上一张图片,例如我希望用户附上第一个按钮,身份证副本,然后是另一个按钮,付款证明副本(图片)。

我的java代码

 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 = "kondja99@gmail.com";
        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();
    }
}

将您的 sendEmail 函数更改为以下内容:

private void sendEmail() {
    try {
        String recipient = "kondja99@gmail.com";
        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();
    }
}

正如评论中有人指出的那样,您的 MIME 类型无效(反向)。最安全的选择是使用 "*/*"。您还需要对 Intent 操作使用 ACTION_SEND_MULTIPLE,因为您要附加多个 Uris。