Saturday, August 4, 2012

Send email with Image by starting activity using Intent of ACTION_SEND

In the previous posts "Send email using Intent.ACTION_SEND" and "Select Image using Android build-in Gallery" demonstrate how to send text email and load image by starting activity using Intent. In this exercise, both function are work together to send email with photos attached.

Send email with Image by starting activity using Intent of ACTION_SEND


To attach image in email intent, call putExtra() to attach the image uri, and call setType() to set type of "image/png".

package com.exercise.AndroidEMail;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {
 
 EditText edittextEmailAddress;
    EditText edittextEmailSubject;
    EditText edittextEmailText;
    TextView textImagePath;
    Button buttonSelectImage;
    Button buttonSendEmail_intent;
    
    final int RQS_LOADIMAGE = 0;
    final int RQS_SENDEMAIL = 1;
    
    Uri imageUri = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        edittextEmailAddress = (EditText)findViewById(R.id.email_address);
        edittextEmailSubject = (EditText)findViewById(R.id.email_subject);
        edittextEmailText = (EditText)findViewById(R.id.email_text);
        textImagePath = (TextView)findViewById(R.id.imagepath);
        buttonSelectImage = (Button)findViewById(R.id.selectimage);
        buttonSendEmail_intent = (Button)findViewById(R.id.sendemail_intent);
        
        buttonSelectImage.setOnClickListener(buttonSelectImageOnClickListener);
        buttonSendEmail_intent.setOnClickListener(buttonSendEmail_intentOnClickListener);
    }

    OnClickListener buttonSelectImageOnClickListener
    = new OnClickListener(){

  @Override
  public void onClick(View arg0) {
   Intent intent = new Intent(Intent.ACTION_PICK,
     android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
   startActivityForResult(intent, RQS_LOADIMAGE);
  }};
  
 OnClickListener buttonSendEmail_intentOnClickListener
 = new OnClickListener(){
  
  @Override
  public void onClick(View arg0) {
   String emailAddress = edittextEmailAddress.getText().toString();
   String emailSubject = edittextEmailSubject.getText().toString();
   String emailText = edittextEmailText.getText().toString();
   String emailAddressList[] = {emailAddress};
   
   Intent intent = new Intent(Intent.ACTION_SEND); 
   
   intent.putExtra(Intent.EXTRA_EMAIL, emailAddressList);  
   intent.putExtra(Intent.EXTRA_SUBJECT, emailSubject); 
   intent.putExtra(Intent.EXTRA_TEXT, emailText); 
   
   if(imageUri != null){
    intent.putExtra(Intent.EXTRA_STREAM, imageUri);
    intent.setType("image/png");
   }else{
    intent.setType("plain/text");
   }
   
   startActivity(Intent.createChooser(intent, "Choice App to send email:"));
    
  }};

 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  // TODO Auto-generated method stub
  super.onActivityResult(requestCode, resultCode, data);
  
  if (resultCode == RESULT_OK){
   switch(requestCode){
   case RQS_LOADIMAGE:
    imageUri = data.getData();
    textImagePath.setText(imageUri.toString());
    break;
   case RQS_SENDEMAIL:
    
    break;
   }
    
  }
 }

    
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   >
<TextView 
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="@string/hello_world"
   />
<TextView 
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="Enter email address:"
   />
<EditText 
   android:id="@+id/email_address"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:inputType="textEmailAddress"
   />
<TextView 
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="Enter email Subject:"
   />
<EditText 
   android:id="@+id/email_subject"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:inputType="textEmailSubject"
   />
<TextView 
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="Enter Text:"
   />
<EditText 
   android:id="@+id/email_text"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   />
<TextView
    android:id="@+id/imagepath"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />
<Button
    android:id="@+id/selectimage"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Select image"
    />
<Button 
   android:id="@+id/sendemail_intent"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text=" Send email using Intent.ACTION_SEND "
   />
</LinearLayout>


Download the files.


Related:
- Share with photo using ShareActionProvider
- Start activity to send multi images attached, with action of ACTION_SEND_MULTIPLE

10 comments:

Unknown said...

Thank you so much. I have been looking for something like this for months. Is there any way to allow for more then one photo attachment? Would like to be able to choose more then one.
Thanks

Erik said...

hello Alex P,

please read Send email with Image by starting activity using Intent of ACTION_SEND.

Unknown said...

Awesome thanks so much i dont know how i missed that. So this does basicly everything i need. All I have to figure out is how to make the enter email address and subject a drop downlist. Any ideas? Would love your help. You have awesome guides :)

Unknown said...

I would be willing to pay if you could help make the to and subject fields a drop down.

thanks

Erik said...

hello Alex P,

Do you means both email address and subject from a pre-defined drop-down selection (Spinner)?

Unknown said...

yes that is correct

Erik said...

hello Alex P,

please read email send images exercise, with spinner of pre-defined subjects..

Anonymous said...

Dobrý deň veľmi pekné webb !! Guy .. Beautiful ..
Kúzelný .. budem označovať svoje stránok a vziať kanály i ?
že som spokojný vyzvedať početný užitočné informácie
tu v príspevok , pogrebujeme zacvičiť ďalšie stratégie na tomto ohľade vďaka pre
zdieľanie. . . . . .

Zastavte sa moje web-stránok ...

Unknown said...

it takes images from device. I want to get it from asset. What changes should i made?

Unknown said...

what to add in android manifest