Android App Take/ Email Photo

2019-08-23 23:33发布

问题:

I am currently writing an app that within a certain activity, we want the user to be able to take and email a photo to a desired email address. I am able to do both of these (take a photo, and send a photo) separately, BUT when I run them together, the email client list comes up over the camera... I cant seem to figure out why it is not running after the camera itself.. Any help?

***Here is what I have now:

public class PhotoHandler extends Activity {

private final static int TAKE_PHOTO_CODE = 1;
File downloadedPic = null;
Intent in;

@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.mnwv_main);

  downloadedPic = takeandReturn(this);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{

  try {            
      Intent picMessageIntent = new Intent(android.content.Intent.ACTION_SEND);            
      picMessageIntent.setType("image/jpeg");
      picMessageIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(downloadedPic));
        picMessageIntent.putExtra(Intent.EXTRA_EMAIL  , new String[]{});
      picMessageIntent.putExtra(Intent.EXTRA_SUBJECT, "MNWV - Check Out This Photo!");
        picMessageIntent.putExtra(Intent.EXTRA_TEXT   , "*** Please Describe the Photo Taken Below (Include Your Name, Location, etc.)... ***");
      startActivity(Intent.createChooser(picMessageIntent, "Send Picture Using: ")); 
  } catch (Exception e) {
      Log.e("TAG", "sendPictureMessage() failed to start activity.", e);
      Toast.makeText(this, "No handler", Toast.LENGTH_LONG).show();
  } 
}  

回答1:

You must use startActivityForResult for taking the photo. After that you must use onActivityResult to send email:

   @Override
   public void onActivityResult(int requestCode, int resultCode, Intent data)
   {
       // TODO: Test for requestCode and resultCode
       try {            
           Intent picMessageIntent = new Intent(android.content.Intent.ACTION_SEND);            
           picMessageIntent.setType("image/jpeg");
           picMessageIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(downloadedPic));
           startActivity(Intent.createChooser(picMessageIntent, "Send Picture Using: "));
       } catch (Exception e) {
           Log.e("TAG", "sendPictureMessage() failed to start activity.", e);
           Toast.makeText(this, "No handler", Toast.LENGTH_LONG).show();
       }
   }

Hope it will help.