Earlier i did Select image from Gallery and attach to and email,see here
but this time i am allowing user to capture an image using inbuilt camera and then want to attach, captured image to an email...
I have written code for both, to capture an Image and to send an EMail, please see my code below: public class CameraActivity extends Activity implements MediaScannerConnectionClient {
private File myImageFile = null;
private Uri myPicture = null;
private MediaScannerConnection conn = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
ImageButton buttonCaptureImage = (ImageButton) findViewById(R.id.btnCaptureImage);
buttonCaptureImage.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
captureImage();
}
});
}
public void captureImage()
{
myImageFile = new File(Environment.getExternalStorageDirectory()+"/CapturedImage.jpg");
myImageFile.delete();
myPicture = Uri.fromFile(myImageFile);
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(MediaStore.EXTRA_OUTPUT, myPicture);
startActivity(i);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode==0 && resultCode==Activity.RESULT_OK)
{
startScan();
}
}
// to attach an image to email
public void sendImage() {
// TODO Auto-generated method stub
Intent i = new Intent(Intent.ACTION_SEND);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/CapturedImage.jpg"));
i.putExtra(Intent.EXTRA_EMAIL, new String[]{"recepient@mail.cin"});
i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
i.putExtra(Intent.EXTRA_TEXT, "body of email");
startActivity(Intent.createChooser(i,"Send email via:"));
}
private void startScan()
{
if(conn !=null)
{
conn.disconnect();
}
if(myImageFile.isFile()) {
conn = new MediaScannerConnection(this, this);
conn.connect();
}
else {
Toast.makeText(this,
"Image does not exist ?",
Toast.LENGTH_SHORT).show();
}
}
@Override
public void onMediaScannerConnected() {
conn.scanFile(myImageFile.getPath(), null);
}
@Override
public void onScanCompleted(String path, Uri uri) {
try {
if (uri != null) {
sendImage();
}
} finally {
conn.disconnect();
conn = null;
}
}
To attach image to an Email in onActivityResult(...) I am using below method in onScanCompleted(....) but still not resolved
// to attach an image to email
public void sendImage() {
// TODO Auto-generated method stub
Intent i = new Intent(Intent.ACTION_SEND);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/CapturedImage.jpg"));
i.putExtra(Intent.EXTRA_EMAIL, new String[]{"recepient@mail.cin"});
i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
i.putExtra(Intent.EXTRA_TEXT, "body of email");
startActivity(Intent.createChooser(i,"Send email via:"));
}