I am building a small Android app that lets people donate food through email. I am having issues with taking a picture and sending it as an email attachment. I know I am initiating two different activities simultaneously and if that's the issue why the app crashes, could there be a way to set the type of intent so as to allow it to use different types of files or content (picture and text in this case)? Or any other suggestions?
Java:
package com.example.android.foodshare;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class Donation extends AppCompatActivity implements View.OnClickListener {
Button submit, open_cam;
EditText company_name;
EditText phone_number;
EditText location;
EditText foodType;
EditText quantity;
ImageView the_food;
File pic;
String companyNameString;
String phoneNumberString;
String locationString;
String foodTypeString;
String quantityString;
String emailTo = "example@example.com";
String emailContent;
String emailSubject;
static final int REQUEST_IMAGE_CAPTURE = 0;
Bitmap imageBitmap;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.donation);
submit = (Button) findViewById(R.id.bSubmit);
open_cam = (Button) findViewById(R.id.bCam);
the_food = (ImageView) findViewById(R.id.ivFood);
company_name = (EditText) findViewById(R.id.etCompName);
phone_number = (EditText) findViewById(R.id.etPhone);
location = (EditText) findViewById(R.id.etLocation);
foodType = (EditText) findViewById(R.id.etFoodType);
quantity = (EditText) findViewById(R.id.etQuantity);
submit.setOnClickListener(this);
open_cam.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bCam:
//more pic handling
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
break;
case R.id.bSubmit:
companyNameString = company_name.getText().toString();
phoneNumberString = phone_number.getText().toString();
locationString = location.getText().toString();
foodTypeString = foodType.getText().toString();
quantityString = quantity.getText().toString();
emailContent = "You have received a Food Pickup Request. Please find details below:\n\n" +
"Name of Organization (or Individual): " + companyNameString + "\n" +
"Telephone Number: " + phoneNumberString + "\n" +
"Location: " + locationString + "\n" +
"Available Food Type: " + foodTypeString + "\n" +
"Quantity Available: " + quantityString;
emailSubject = "New Food Pickup Request From " + companyNameString;
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{emailTo});
emailIntent.putExtra(Intent.EXTRA_SUBJECT, emailSubject);
emailIntent.putExtra(Intent.EXTRA_TEXT, emailContent);
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(pic));
// new line added
emailIntent.setType("image/png");
startActivity(Intent.createChooser(emailIntent,"Share you on the jobing"));
//need this to prompt email client only
emailIntent.setType("message/rfc822");
try {
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(Donation.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}
break;
} // end of switch statement
};
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
imageBitmap = (Bitmap) extras.get("data");
the_food.setImageBitmap(imageBitmap);
try {
File root = Environment.getExternalStorageDirectory();
if (root.canWrite()){
pic = new File(root, "pic.png");
FileOutputStream out = new FileOutputStream(pic);
imageBitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
}
} catch (IOException e) {
Log.e("BROKEN", "Could not write file " + e.getMessage());
}
}
}
}
Stack trace:
05-11 19:56:00.415 2787-2787/com.example.android.foodshare E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.android.foodshare, PID: 2787 java.lang.NullPointerException: file at android.net.Uri.fromFile(Uri.java:452) at com.example.android.foodshare.Donation.onClick(Donation.java:94) at android.view.View.performClick(View.java:5637) at android.view.View$PerformClick.run(View.java:22429) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)