I am trying to create an App that takes a picture when a button is pressed and then attaches it to email when a send button is pressed. The code I have shows that the picture is attached to the email, but the attachment is not sent when I check the email I sent it to.The image is not saved in the SD card too.
I call the initialize function in my onCreate() to initialize variable.Also I dont know where to put the SaveImage() function which has to save the image once it’s taken. I put it in the onActivityResult() but does not work.
Thanks in advance!
private void initialize() {
// TODO Auto-generated method stub
camera = (Button) findViewById(R.id.Picture);
sendEmail = (Button) findViewById(R.id.SendMessage);
iv = (ImageView) findViewById(R.id.ImageReturn);
MessageTyped = (EditText) findViewById(R.id.MessageField);
sendEmail.setOnClickListener(this);
camera.setOnClickListener(this);
pngDir = new File(Environment.getExternalStorageDirectory(),
// Loose convention inferred from app examples
"My Images");
if (!pngDir.exists())
pngDir.mkdirs();
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.Picture:
i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
SaveImage();//<------------------WHERE DOES THIS GO?
startActivityForResult(i, cameraData);
break;
case R.id.SendMessage:
EditTextToString();
EmailIntent = new Intent(android.content.Intent.ACTION_SEND);
EmailIntent.putExtra(Intent.EXTRA_EMAIL,
new String[] { "bipush.osti@gmail.com" });
EmailIntent.putExtra(android.content.Intent.EXTRA_TEXT,
MessageToBeReceived);
// EmailIntent.setType("message/rfc822");
EmailIntent.setType("image/jpeg");
EmailIntent.putExtra(Intent.EXTRA_STREAM, pngUri);
startActivity(Intent.createChooser(EmailIntent,
"Choose an Email client :"));
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
bmp = (Bitmap) extras.get("data");
iv.setImageBitmap(bmp);
}
}
private void EditTextToString() {
MessageToBeReceived = MessageTyped.getText().toString();
}
private void SaveImage() {
File pngFile = new File(pngDir, "jetsam.jpeg");
// Save file encoded as PNG
pngUri = Uri.fromFile(pngFile);
}
See documentation of ACTION_IMAGE_CAPTURE! If you didn’t gave EXTRA_OUTPUT (uri with location where image should be saved), then in result you will get “small sized image is returned as a Bitmap object”.
So just add uri to intent when requesting a picture and pass same uri to ACTION_SEND: