-2

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:"));   
    }
Community
  • 1
  • 1
Sun
  • 6,768
  • 25
  • 76
  • 131

3 Answers3

1

You need to put code of sendImage in onScanCompleted like this :

@Override
    public void onScanCompleted(String path, Uri uri) {
        try {
            if (uri != null) {
                sendImage();
            }
        } finally {
            conn.disconnect();
            conn = null;
        } 
    }

For better understanding, have a look here

Community
  • 1
  • 1
SweetWisher ツ
  • 7,296
  • 2
  • 30
  • 74
0

attach image to an email

Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("text/html");
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "File attached");
Uri uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "image_Path"));
emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
Mohammad Rababah
  • 1,730
  • 4
  • 17
  • 32
0

I set content type image/* when I want to share Image with text, also I create Uri object from file descriptor not from path string.

Uri imageFileUri = Uri.fromFile(imageFile); //file descriptor should be valid

Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/*");
shareIntent.putExtra(Intent.EXTRA_TEXT,"Some text");
shareIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"recepient@mail.cin"});
shareIntent.putExtra(Intent.EXTRA_STREAM, imageFileUri);
shareIntent.putExtra(Intent.EXTRA_SUBJECT,"Subject");
startActivity(shareIntent);

It always works good for me

Sergey Pekar
  • 8,555
  • 7
  • 47
  • 54