创建一个新工程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="邮箱地址如:[email protected]" />  <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



更多相关文章

  1. 转:在Eclipse中查看Android SDK的源代码
  2. 使用Java代码在应用层获取Android系统属性
  3. android源代码分布结构
  4. Android bluetooth介绍(二): android 蓝牙代码架构及其uart 到rfcom
  5. Android系统手机重启与恢复出产设置源代码跟踪

随机推荐

  1. Android的Activity实时刷新
  2. Android——Android(安卓)Studio开发环境
  3. 在android中创建bitmap避免内存不足的方
  4. Android(安卓)视频播放
  5. Android获得当前系统时间、星期几、周几
  6. 设置 Toolbar(ActionBar) 上的按钮颜色
  7. android中调用金山词霸
  8. Android使用代码实现RelativeLayout,Linea
  9. Android(安卓)5.1 open data flow 数据开
  10. Android(安卓)studio+真机 运行报错[INST