【Android笔记】Androd手机拍照或从相册选取图片作为头像

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

参考原文:http://www.open-open.com/lib/view/open1428569177447.html

你可能感兴趣的:(android)