I am writing a scavenger hunt type app that allows for the user to take one or two photos of a bonus location and then submit those images via email. However, I can't seem to get it to put both images in the email, it only gets the most recent one.
I found this answer: Cant send email with multiple attachment in android programmatically, which seems to say that I must do this using an array, but the example there doesn't seem to match at all what I'm doing so I'm not certain how to make the array.
Below is my current code. Could someone tell me how to make my two EXTRA_STREAMS into the requisite array (or point me in the correct direction if that is not the proper fix)?
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..."));
}
}