创建一个新工程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
|