使用带有两个附件的已发送电子邮件意图

时间:2019-01-10 08:13:12

标签: android

我正在编写一个寻宝游戏类型的应用程序,该应用程序允许用户拍摄奖励位置的一两张照片,然后通过电子邮件提交这些图像。但是,我似乎无法将两个图像都放入电子邮件中,只能获取最新的图像。

我找到了这个答案:Cant send email with multiple attachment in android programmatically,这似乎表明我必须使用数组来执行此操作,但是那里的示例似乎与我正在执行的操作完全不匹配,因此我不确定如何制作数组。

下面是我当前的代码。有人可以告诉我如何将我的两个EXTRA_STREAMS放入必需的数组中(或者如果这不是正确的解决方法,请指向正确的方向)?

package net.tommyc.android.tourofhonor;

import android.Manifest;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import java.io.File;
import java.io.IOException;

public class captureBonus extends AppCompatActivity {

    /**
     * Opens an already installed Camera application
     */
    static final int REQUEST_TAKE_PHOTO = 1;
    int riderNumToH = 479;
    int pillionNumToH = 000;
    String submissionEmailAddress = "me@tommyc.net";
    Button btnTakeMainPic;
    Button btnSubmitBonus;
    ImageView imageViewMain;
    ImageView imageViewSecondary;
    int tappedImageView = 3;
    File mainPhotoUri = null;
    File secondaryPhotoUri = null;
    /**
     * Saves the full size image to the public photo directory (similar to the Camera Roll on iOS)
     * * saveImage(imageName: "2018_\(riderNumToH)_\(bonusCodeLabel.text!)_1.jpg")
     */

    String mainPhotoPath;
    String secondaryPhotoPath;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_capture_bonus);
        if (Build.VERSION.SDK_INT >= 23) {
            requestPermissions(new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 2);
        }

        btnSubmitBonus = findViewById(R.id.btnSubmitBonus);
        btnSubmitBonus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dispatchSubmitBonusIntent();
            }
        });
        imageViewMain = findViewById(R.id.bonusMainImage);
        imageViewMain.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                tappedImageView = 0;
                dispatchTakeMainPictureIntent();
                Log.v("User Action", "Main Image Tapped");
            }
        });
        imageViewSecondary = findViewById(R.id.bonusSecondaryImage);
        imageViewSecondary.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                tappedImageView = 1;
                dispatchTakeMainPictureIntent();
                Log.v("User Action", "Main Image Tapped");
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == 1) {

                if (tappedImageView == 0) {
                    Bitmap bitmap = BitmapFactory.decodeFile(mainPhotoPath);
                    imageViewMain.setImageBitmap(bitmap);
                } else if (tappedImageView == 1) {
                    Bitmap bitmap = BitmapFactory.decodeFile(secondaryPhotoPath);
                    imageViewSecondary.setImageBitmap(bitmap);
                } else {
                    Log.w("ERROR", "onActivityResult: valid view ID not found (" + tappedImageView + ")");
                }

            }
        }
    }

    private void dispatchTakeMainPictureIntent() {
        Intent takeMainPictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        // Ensure that there's a camera activity to handle the intent
        if (takeMainPictureIntent.resolveActivity(getPackageManager()) != null) {
            // Create the File where the photo should go
            File photoFile = null;
            try {
                photoFile = createImageFile();
                if (tappedImageView == 0) {
                    mainPhotoUri = createImageFile();
                } else if (tappedImageView == 1) {
                    secondaryPhotoUri = createImageFile();
                }
            } catch (IOException ex) {
                // Error occurred while creating the File
                Log.e("fileCreationError", "An error occurred while creating the image file.");
            }
            // Continue only if the File was successfully created
            if (photoFile != null) {
                Uri photoURI = FileProvider.getUriForFile(captureBonus.this, "net.tommyc.android.tourofhonor", photoFile);
                takeMainPictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                startActivityForResult(takeMainPictureIntent, REQUEST_TAKE_PHOTO);
            }
        }
    }

    private File createImageFile() throws IOException {
        // Create an image file name
        String imagePath = Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES).toString();
        String mainImageFileName = "2019_" + riderNumToH + "_BonusCode_1.jpg";
        String secondaryImageFileName = "2019_" + riderNumToH + "_BonusCode_2.jpg";
        if (tappedImageView == 0) {
            File capturedImage = new File(imagePath, mainImageFileName);
            mainPhotoPath = capturedImage.getAbsolutePath();
            return capturedImage;
        } else if (tappedImageView == 1) {
            File capturedImage = new File(imagePath, secondaryImageFileName);
            secondaryPhotoPath = capturedImage.getAbsolutePath();
            return capturedImage;
        } else {
            Log.w("ERROR", "createImageFile: valid view ID not found (" + tappedImageView + ")");
        }
        return null;
    }

    /**
     * Submits the bonus images via email.
     */
    private void dispatchSubmitBonusIntent() {
        Intent sendEmailIntent = new Intent(Intent.ACTION_SEND);
        sendEmailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        sendEmailIntent.setType("plain/text");
        sendEmailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{submissionEmailAddress});
        sendEmailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "2019_" + riderNumToH + "_BonusCode");
        sendEmailIntent.putExtra(Intent.EXTRA_TEXT, "Sent from TOH App\nAndroid Version 0.3.076");
        if (mainPhotoPath != null) {
            sendEmailIntent.putExtra(android.content.Intent.EXTRA_STREAM, FileProvider.getUriForFile(captureBonus.this, "net.tommyc.android.tourofhonor", mainPhotoUri));
            Log.v("MainImageFound", mainPhotoPath + "|" + mainPhotoUri);
            if (secondaryPhotoPath != null) {
                sendEmailIntent.putExtra(android.content.Intent.EXTRA_STREAM, FileProvider.getUriForFile(captureBonus.this, "net.tommyc.android.tourofhonor", secondaryPhotoUri));
                Log.v("SecondaryImageFound", secondaryPhotoPath + "|" + secondaryPhotoUri);
            } else {
                Log.e("NoImageFound", "Image Not Found");
            }
        }
        this.startActivity(Intent.createChooser(sendEmailIntent, "Sending email..."));
    }
}

1 个答案:

答案 0 :(得分:0)

您应该将Uri的所有文件放入var data = getComputedStyle($('span')[0]).background; console.log(data); ,然后由Arraylist<Parcelable>放入意图