在android的开发过程中,经常遇到设置用户头像以及裁剪图像大小的功能。昨天我遇到了设置用户头像的功能,开始不知道怎么搞,在技术群里问也没人回答,就研究了微信、以及4399游戏中心用户设置头像的功能,了解到用户设置图像的过程,就是两步点击设置头像,选择头像资源的来源,相册和相机两个来得到。
闲话说的有点多了,来说说技术点吧。其实也比较简单,就是通过Intent来启动相册或者相机就可以了。
看下效果图:

 

 

 

 

启动相册以及相机关键代码:

 

  1. switch (which) {  
  2.                     case 0:
  3. // 打开相册
  4.                         Intent intentFromGallery = new Intent();  
  5.                         intentFromGallery.setType("p_w_picpath/*"); // 设置文件类型  
  6.                         intentFromGallery  
  7.                                 .setAction(Intent.ACTION_GET_CONTENT);  
  8.                         startActivityForResult(intentFromGallery,  
  9.                                 IMAGE_REQUEST_CODE);  
  10.                         break;  
  11.                     case 1:  
  12.   // 打开相机
  13.                         Intent intentFromCapture = new Intent(  
  14.                                 MediaStore.ACTION_IMAGE_CAPTURE);  
  15.                         // 判断存储卡是否可以用,可用进行存储  
  16.                         if (Tools.hasSdcard()) {  
  17.  
  18.                             intentFromCapture.putExtra(  
  19.                                     MediaStore.EXTRA_OUTPUT,  
  20.                                     Uri.fromFile(new File(Environment  
  21.                                             .getExternalStorageDirectory(),  
  22.                                             IMAGE_FILE_NAME)));  
  23.                         }  
  24.  
  25.                         startActivityForResult(intentFromCapture,  
  26.                                 CAMERA_REQUEST_CODE);  
  27.                         break;  
  28.                     } 

打开系统自带的裁剪功能代码:

 

  1. /**  
  2.      * 裁剪图片方法实现  
  3.      *   
  4.      * @param uri  
  5.      */ 
  6.     public void startPhotoZoom(Uri uri) {  
  7.  
  8.         Intent intent = new Intent("com.android.camera.action.CROP");  
  9.         intent.setDataAndType(uri, "p_w_picpath/*");  
  10.         // 设置裁剪  
  11.         intent.putExtra("crop""true");  
  12.         // aspectX aspectY 是宽高的比例  
  13.         intent.putExtra("aspectX"1);  
  14.         intent.putExtra("aspectY"1);  
  15.         // outputX outputY 是裁剪图片宽高  
  16.         intent.putExtra("outputX"320);  
  17.         intent.putExtra("outputY"320);  
  18.         intent.putExtra("return-data"true);  
  19.         startActivityForResult(intent, 2);  
  20.     }  

完整实现代码如下:

  1. package com.xzw.picture;  
  2.  
  3. import java.io.File;  
  4.  
  5. import com.xzw.utils.Tools;  
  6.  
  7. import android.app.Activity;  
  8. import android.app.AlertDialog;  
  9. import android.content.DialogInterface;  
  10. import android.content.Intent;  
  11. import android.graphics.Bitmap;  
  12. import android.graphics.drawable.BitmapDrawable;  
  13. import android.graphics.drawable.Drawable;  
  14. import android.net.Uri;  
  15. import android.os.Bundle;  
  16. import android.os.Environment;  
  17. import android.provider.MediaStore;  
  18. import android.view.View;  
  19. import android.view.Window;  
  20. import android.widget.ImageView;  
  21. import android.widget.RelativeLayout;  
  22. import android.widget.Toast;  
  23.  
  24. /**  
  25.  *   
  26.  * @author XuZhiwei (xuzw13@gmail.com)  
  27.  * Create at 2012-8-17 上午10:14:40  
  28.  */ 
  29. public class MainActivity extends Activity {  
  30.     /*组件*/ 
  31.     private RelativeLayout switchAvatar;  
  32.     private ImageView faceImage;  
  33.  
  34.     private String[] items = new String[] { "选择本地图片""拍照" };  
  35.     /*头像名称*/ 
  36.     private static final String IMAGE_FILE_NAME = "faceImage.jpg";  
  37.       
  38.     /* 请求码*/ 
  39.     private static final int IMAGE_REQUEST_CODE = 0;  
  40.     private static final int CAMERA_REQUEST_CODE = 1;  
  41.     private static final int RESULT_REQUEST_CODE = 2;  
  42.  
  43.     @Override 
  44.     public void onCreate(Bundle savedInstanceState) {  
  45.         super.onCreate(savedInstanceState);  
  46.         requestWindowFeature(Window.FEATURE_NO_TITLE); // 去掉标题  
  47.         setContentView(R.layout.main);  
  48.         switchAvatar = (RelativeLayout) findViewById(R.id.switch_face_rl);  
  49.         faceImage = (ImageView) findViewById(R.id.face);  
  50.         //设置事件监听  
  51.         switchAvatar.setOnClickListener(listener);  
  52.     }  
  53.      
  54.     private View.OnClickListener listener = new View.OnClickListener() {  
  55.  
  56.         @Override 
  57.         public void onClick(View v) {  
  58.             showDialog();  
  59.         }  
  60.     };  
  61.     /**  
  62.      * 显示选择对话框  
  63.      */ 
  64.     private void showDialog() {  
  65.           
  66.         new AlertDialog.Builder(this)  
  67.                 .setTitle("设置头像")  
  68.                 .setItems(items, new DialogInterface.OnClickListener() {  
  69.  
  70.                     @Override 
  71.                     public void onClick(DialogInterface dialog, int which) {  
  72.                         switch (which) {  
  73.                         case 0:  
  74.                             Intent intentFromGallery = new Intent();  
  75.                             intentFromGallery.setType("p_w_picpath/*"); // 设置文件类型  
  76.                             intentFromGallery  
  77.                                     .setAction(Intent.ACTION_GET_CONTENT);  
  78.                             startActivityForResult(intentFromGallery,  
  79.                                     IMAGE_REQUEST_CODE);  
  80.                             break;  
  81.                         case 1:  
  82.  
  83.                             Intent intentFromCapture = new Intent(  
  84.                                     MediaStore.ACTION_IMAGE_CAPTURE);  
  85.                             // 判断存储卡是否可以用,可用进行存储  
  86.                             if (Tools.hasSdcard()) {  
  87.  
  88.                                 intentFromCapture.putExtra(  
  89.                                         MediaStore.EXTRA_OUTPUT,  
  90.                                         Uri.fromFile(new File(Environment  
  91.                                                 .getExternalStorageDirectory(),  
  92.                                                 IMAGE_FILE_NAME)));  
  93.                             }  
  94.  
  95.                             startActivityForResult(intentFromCapture,  
  96.                                     CAMERA_REQUEST_CODE);  
  97.                             break;  
  98.                         }  
  99.                     }  
  100.                 })  
  101.                 .setNegativeButton("取消"new DialogInterface.OnClickListener() {  
  102.  
  103.                     @Override 
  104.                     public void onClick(DialogInterface dialog, int which) {  
  105.                         dialog.dismiss();  
  106.                     }  
  107.                 }).show();  
  108.  
  109.     }  
  110.       
  111.     @Override 
  112.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  113.      //结果码不等于取消时候 if (resultCode != RESULT_CANCELED) {   
  114.  switch (requestCode) {  
  115.         case IMAGE_REQUEST_CODE:  
  116.             startPhotoZoom(data.getData());  
  117.             break;  
  118.         case CAMERA_REQUEST_CODE:  
  119.             if (Tools.hasSdcard()) {  
  120.                 File tempFile = new File(  
  121.                         Environment.getExternalStorageDirectory()  
  122.                                 + IMAGE_FILE_NAME);  
  123.                 startPhotoZoom(Uri.fromFile(tempFile));  
  124.             } else {  
  125.                 Toast.makeText(MainActivity.this"未找到存储卡,无法存储照片!",  
  126.                         Toast.LENGTH_LONG).show();  
  127.             }  
  128.  
  129.             break;  
  130.         case RESULT_REQUEST_CODE:  
  131.             if (data != null) {  
  132.                 getImageToView(data);  
  133.             }  
  134.             break;  
  135.         }  
  136. }
  137.         super.onActivityResult(requestCode, resultCode, data);  
  138.     }  
  139.  
  140.     /**  
  141.      * 裁剪图片方法实现  
  142.      *   
  143.      * @param uri  
  144.      */  
  145.     public void startPhotoZoom(Uri uri) {  
  146.  
  147.         Intent intent = new Intent("com.android.camera.action.CROP");  
  148.         intent.setDataAndType(uri, "p_w_picpath/*");  
  149.         // 设置裁剪  
  150.         intent.putExtra("crop""true");  
  151.         // aspectX aspectY 是宽高的比例  
  152.         intent.putExtra("aspectX"1);  
  153.         intent.putExtra("aspectY"1);  
  154.         // outputX outputY 是裁剪图片宽高  
  155.         intent.putExtra("outputX"320);  
  156.         intent.putExtra("outputY"320);  
  157.         intent.putExtra("return-data"true);  
  158.         startActivityForResult(intent, 2);  
  159.     }  
  160.  
  161.     /**  
  162.      * 保存裁剪之后的图片数据  
  163.      *   
  164.      * @param picdata  
  165.      */  
  166.     private void getImageToView(Intent data) {  
  167.         Bundle extras = data.getExtras();  
  168.         if (extras != null) {  
  169.             Bitmap photo = extras.getParcelable("data");  
  170.             Drawable drawable = new BitmapDrawable(photo);  
  171.             faceImage.setImageDrawable(drawable);  
  172.         }  
  173.     }  
  174.  
  175. }  

 

还有一个工具类:

 

  1. package com.xzw.utils;  
  2.  
  3. import android.os.Environment;  
  4. /**  
  5.  *   
  6.  * @author XuZhiwei (xuzw13@gmail.com)  
  7.  * Create at 2012-8-17 上午10:14:40  
  8.  */ 
  9. public class Tools {  
  10.     /**  
  11.      * 检查是否存在SDCard  
  12.      * @return  
  13.      */ 
  14.     public static boolean hasSdcard(){  
  15.         String state = Environment.getExternalStorageState();  
  16.         if(state.equals(Environment.MEDIA_MOUNTED)){  
  17.             return true;  
  18.         }else{  
  19.             return false;  
  20.         }  
  21.     }  
  22. }  
以上就是实现的代码!完整的资源包。
刚刚在51论坛发表了这篇,这里再做个笔记哈。