Android手机拍照或选取图库图片作为头像

时间:2022-01-15 14:40:57
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package zhangpgil.photo;
 
import java.io.File;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
 
public class MainActivity extends ActionBarActivity {
 
  /* 头像文件 */
  private static final String IMAGE_FILE_NAME = "temp_head_image.jpg";
 
  /* 请求识别码 */
  private static final int CODE_GALLERY_REQUEST = 0xa0;
  private static final int CODE_CAMERA_REQUEST = 0xa1;
  private static final int CODE_RESULT_REQUEST = 0xa2;
 
  // 裁剪后图片的宽(X)和高(Y),480 X 480的正方形。(生成bitmap貌似有时要报错?可试下把大小弄小点)
  private static int output_X = 480;
  private static int output_Y = 480;
 
  private ImageView headImage = null;
 
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
 
    headImage = (ImageView) findViewById(R.id.imageView);
 
    Button buttonLocal = (Button) findViewById(R.id.buttonLocal);
    buttonLocal.setOnClickListener(new View.OnClickListener() {
 
      @Override
      public void onClick(View v) {
        choseHeadImageFromGallery();
      }
    });
 
    Button buttonCamera = (Button) findViewById(R.id.buttonCamera);
    buttonCamera.setOnClickListener(new View.OnClickListener() {
 
      @Override
      public void onClick(View v) {
        choseHeadImageFromCameraCapture();
      }
    });
  }
 
  // 从本地相册选取图片作为头像
  private void choseHeadImageFromGallery() {
    Intent intentFromGallery = new Intent();
    // 设置文件类型
    intentFromGallery.setType("image/*");
    intentFromGallery.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(intentFromGallery, CODE_GALLERY_REQUEST);
  }
 
  // 启动手机相机拍摄照片作为头像
  private void choseHeadImageFromCameraCapture() {
    Intent intentFromCapture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
 
    // 判断存储卡是否可用,存储照片文件
    if (hasSdcard()) {
      intentFromCapture.putExtra(MediaStore.EXTRA_OUTPUT, Uri
          .fromFile(new File(Environment
              .getExternalStorageDirectory(), IMAGE_FILE_NAME)));
    }
 
    startActivityForResult(intentFromCapture, CODE_CAMERA_REQUEST);
  }
 
  @Override
  protected void onActivityResult(int requestCode, int resultCode,
      Intent intent) {
 
    // 用户没有进行有效的设置操作,返回
    if (resultCode == RESULT_CANCELED) {
      Toast.makeText(getApplication(), "取消", Toast.LENGTH_LONG).show();
      return;
    }
 
    switch (requestCode) {
    case CODE_GALLERY_REQUEST:
      cropRawPhoto(intent.getData());
      break;
 
    case CODE_CAMERA_REQUEST:
      if (hasSdcard()) {
        File tempFile = new File(
            Environment.getExternalStorageDirectory(),
            IMAGE_FILE_NAME);
        cropRawPhoto(Uri.fromFile(tempFile));
      } else {
        Toast.makeText(getApplication(), "没有SDCard!", Toast.LENGTH_LONG)
            .show();
      }
 
      break;
 
    case CODE_RESULT_REQUEST:
      if (intent != null) {
        setImageToHeadView(intent);
      }
 
      break;
    }
 
    super.onActivityResult(requestCode, resultCode, intent);
  }
 
  /**
   * 裁剪原始的图片
   */
  public void cropRawPhoto(Uri uri) {
 
    Intent intent = new Intent("com.android.camera.action.CROP");
    intent.setDataAndType(uri, "image/*");
 
    // 设置裁剪
    intent.putExtra("crop", "true");
 
    // aspectX , aspectY :宽高的比例
    intent.putExtra("aspectX", 1);
    intent.putExtra("aspectY", 1);
 
    // outputX , outputY : 裁剪图片宽高
    intent.putExtra("outputX", output_X);
    intent.putExtra("outputY", output_Y);
    intent.putExtra("return-data", true);
 
    startActivityForResult(intent, CODE_RESULT_REQUEST);
  }
 
  /**
   * 提取保存裁剪之后的图片数据,并设置头像部分的View
   */
  private void setImageToHeadView(Intent intent) {
    Bundle extras = intent.getExtras();
    if (extras != null) {
      Bitmap photo = extras.getParcelable("data");
      headImage.setImageBitmap(photo);
    }
  }
 
  /**
   * 检查设备是否存在SDCard的工具方法
   */
  public static boolean hasSdcard() {
    String state = Environment.getExternalStorageState();
    if (state.equals(Environment.MEDIA_MOUNTED)) {
      // 有存储的SDCard
      return true;
    } else {
      return false;
    }
  }
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" >
 
  <ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher" />
 
  <Button
    android:id="@+id/buttonLocal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="本地相册选取头像" />
 
  <Button
    android:id="@+id/buttonCamera"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="手机拍照选取头像" />
 
</LinearLayout>

以上所述就是本文的全部内容了,希望大家能够喜欢。