android实现发送Email功能(从相册中选择图片作为附件)



创建一个新工程LearnSendEmail

Activity_main.xml代码

<?xml version="1.0" encoding="utf-8"?>

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"

    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    android:orientation="vertical"

    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

  

    <EditText

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:inputType="textEmailAddress"

        android:ems="10"

        android:id="@+id/editTextTo"

        android:hint="邮箱地址如:caizhigui8888@126.com" />

  

    <EditText

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:id="@+id/editTextSubject"

        android:hint="邮件主题subject" />

  

    <ScrollView

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:id="@+id/scrollView"

        android:layout_weight="1">

  

  

        <EditText

            android:layout_width="match_parent"

            android:layout_height="match_parent"

            android:hint="输入邮件内容"

            android:inputType="textMultiLine"

            android:id="@+id/editTextMessage" />

    </ScrollView>

  

    <LinearLayout

        android:orientation="horizontal"

        android:layout_width="match_parent"

        android:layout_height="wrap_content">

  

        <Button

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="发送"

            android:id="@+id/btnSend"

            android:layout_weight="1" />

  

        <Button

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="添加附件"

            android:id="@+id/btnAttachment"

            android:layout_weight="1" />

    </LinearLayout>

  

</LinearLayout>

 

MainActivity.java代码

package com.czg.com.learnsendemail;

  

  import android.content.Intent;

  import android.database.Cursor;

  import android.net.Uri;

  import android.provider.MediaStore;

  import android.support.v7.app.AppCompatActivity;

  import android.os.Bundle;

  import android.util.Log;

  import android.view.View;

  import android.widget.Button;

  import android.widget.EditText;

  import android.widget.Toast;

  

  public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    EditText editTextEmail,editTextSubject,editTextMessage;

    Button btnSend,btnAttachment;

    String email,subject,message,attachmentFile;

    Uri URI=null;

    private  static final int PICK_FROM_GALLERY=101;

    int columnIndex;

  

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

  

        editTextEmail= (EditText) findViewById(R.id.editTextTo);

        editTextSubject= (EditText) findViewById(R.id.editTextSubject);

        editTextMessage= (EditText) findViewById(R.id.editTextMessage);

        btnSend= (Button) findViewById(R.id.btnSend);

        btnAttachment= (Button) findViewById(R.id.btnAttachment);

  

        btnSend.setOnClickListener(this);

        btnAttachment.setOnClickListener(this);

    }

  

    @Override

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        super.onActivityResult(requestCode, resultCode, data);

  

        if(requestCode==PICK_FROM_GALLERY && resultCode==RESULT_OK){

            Uri selectedImage=data.getData();

            String[] filePathColumn={MediaStore.Images.Media.DATA};

  

            Cursor cursor=getContentResolver().query(selectedImage,filePathColumn,null,null,null);

            cursor.moveToFirst();

            columnIndex=cursor.getColumnIndex(filePathColumn[0]);

            attachmentFile=cursor.getString(columnIndex);

            Log.e("附件文件路径:",attachmentFile);

            URI=Uri.parse("file://"+attachmentFile);

            cursor.close();

        }

    }

  

    @Override

    public void onClick(View v) {

        switch (v.getId()){

            case R.id.btnAttachment:

                openGallery();

                break;

            case R.id.btnSend:

                email=editTextEmail.getText().toString();

                subject=editTextSubject.getText().toString();

                message=editTextMessage.getText().toString();

  

                final Intent emailIntent=new Intent(Intent.ACTION_SEND);

                emailIntent.setType("plain/text");

                emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{email});

                emailIntent.putExtra(Intent.EXTRA_SUBJECT,subject);

                if (URI!=null){

                    emailIntent.putExtra(Intent.EXTRA_STREAM,URI);

                }

                emailIntent.putExtra(Intent.EXTRA_TEXT, message);

  

                try{

                    this.startActivity(Intent.createChooser(emailIntent,"发送邮件..."));

  

                }

                catch (Exception e){

                    Toast.makeText(this,"发送失败,可以再试发",Toast.LENGTH_LONG).show();

  

                }

  

  

  

  

  

                break;

        }

  

    }

  

    private void openGallery() {

        Intent intent=new Intent();

        intent.setType("image/*");

        intent.setAction(Intent.ACTION_GET_CONTENT);

        intent.putExtra("return-data", true);

        startActivityForResult(Intent.createChooser(intent,"complete action using"),PICK_FROM_GALLERY);

  

    }

}
源码下载:https://yunpan.cn/crxHsVtSDQeYp  访问密码 5351

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

牵手生活

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值