通过拍照或选择本地图片并剪裁图片设置成头像

来源:互联网 发布:淘宝金牌卖家显示出来 编辑:程序博客网 时间:2024/05/29 17:14
  1. import java.io.File;  
  2.   
  3. import android.app.AlertDialog;  
  4. import android.content.Context;  
  5. import android.content.DialogInterface;  
  6. import android.content.Intent;  
  7. import android.graphics.Bitmap;  
  8. import android.graphics.drawable.BitmapDrawable;  
  9. import android.graphics.drawable.Drawable;  
  10. import android.net.Uri;  
  11. import android.os.Bundle;  
  12. import android.os.Environment;  
  13. import android.provider.MediaStore;  
  14. import android.util.Log;  
  15. import android.view.View;  
  16. import android.widget.Button;  
  17. import android.widget.Toast;  
  18.   
  19. public class CropActivity extends BaseActivity {  
  20.     private Context context;  
  21.     private Button btn_crop;  
  22.     private String[] items = new String[] { "选择本地图片""拍照" };  
  23.       
  24.     /*头像名称*/   
  25.     private static final String IMAGE_FILE_NAME = "faceImage.jpg";   
  26.       
  27.     /* 请求码*/   
  28.     private static final int IMAGE_REQUEST_CODE = 0;    
  29.     private static final int CAMERA_REQUEST_CODE = 1;    
  30.     private static final int RESULT_REQUEST_CODE = 2;    
  31.       
  32.     @Override  
  33.     public void onCreate(Bundle savedInstanceState) {  
  34.         super.onCreate(savedInstanceState);  
  35.         setContentView(R.layout.activity_crop);  
  36.         context=this;  
  37.         initViews();  
  38.     }  
  39.   
  40.       
  41.     @Override  
  42.     protected void onResume() {  
  43.         // TODO Auto-generated method stub  
  44.         super.onResume();  
  45.         initValues();  
  46.     }  
  47.   
  48.     @Override  
  49.     protected void updateViews(Object o) {  
  50.         // TODO Auto-generated method stub  
  51.   
  52.     }  
  53.   
  54.     @Override  
  55.     protected void initViews() {  
  56.         // TODO Auto-generated method stub  
  57.           
  58.         btn_crop=(Button)findViewById(R.id.btn_crop);  
  59.         btn_crop.setOnClickListener(new View.OnClickListener() {  
  60.               
  61.             @Override  
  62.             public void onClick(View v) {  
  63.                 // TODO Auto-generated method stub  
  64.                 showDialog();  
  65.             }  
  66.         });  
  67.           
  68.     }  
  69.   
  70.     @Override  
  71.     protected void initValues() {  
  72.         // TODO Auto-generated method stub  
  73.     }  
  74.   
  75.     @Override  
  76.     protected void initHandler() {  
  77.         // TODO Auto-generated method stub  
  78.   
  79.     }  
  80.       
  81.     private void showDialog() {    
  82.           
  83.         new AlertDialog.Builder(this)    
  84.                 .setTitle("设置头像")    
  85.                 .setItems(items, new DialogInterface.OnClickListener() {    
  86.    
  87.                     @Override   
  88.                     public void onClick(DialogInterface dialog, int which) {    
  89.                         switch (which) {    
  90.                         case 0:    
  91.                             Intent intentFromGallery = new Intent();    
  92.                             intentFromGallery.setType("image/*"); // 设置文件类型    
  93.                             intentFromGallery.setAction(Intent.ACTION_GET_CONTENT);  
  94.                             startActivityForResult(intentFromGallery,IMAGE_REQUEST_CODE);   
  95.                             break;    
  96.                         case 1:    
  97.                             Intent intentFromCapture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  
  98.                             // 判断存储卡是否可以用,可用进行存储    
  99.                             if (hasSdcard()) {    
  100.                                 intentFromCapture.putExtra(MediaStore.EXTRA_OUTPUT,  
  101.                                         Uri.fromFile(new File(Environment.getExternalStorageDirectory(),IMAGE_FILE_NAME)));  
  102.                             }    
  103.                             startActivityForResult(intentFromCapture,CAMERA_REQUEST_CODE);  
  104.                             break;    
  105.                         }    
  106.                     }    
  107.                 })    
  108.                 .setNegativeButton("取消"new DialogInterface.OnClickListener() {    
  109.    
  110.                     @Override   
  111.                     public void onClick(DialogInterface dialog, int which) {    
  112.                         dialog.dismiss();    
  113.                     }    
  114.                 }).show();    
  115.    
  116.     }    
  117.       
  118.     @Override  
  119.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  120.         // 结果码不等于取消时候  
  121.         if (resultCode != RESULT_CANCELED) {  
  122.             switch (requestCode) {  
  123.             case IMAGE_REQUEST_CODE:  
  124.                 startPhotoZoom(data.getData());  
  125.                 break;  
  126.             case CAMERA_REQUEST_CODE:  
  127.                 if (hasSdcard()) {  
  128.                     File tempFile = new File(Environment.getExternalStorageDirectory(),IMAGE_FILE_NAME);  
  129.                     startPhotoZoom(Uri.fromFile(tempFile));  
  130.                 } else {  
  131.                     showToast("未找到存储卡,无法存储照片!");  
  132.                 }  
  133.   
  134.                 break;  
  135.             case RESULT_REQUEST_CODE:  
  136.                 if (data != null) {  
  137.                     setImageToView(data);  
  138.                 }  
  139.                 break;  
  140.             }  
  141.         }  
  142.         super.onActivityResult(requestCode, resultCode, data);  
  143.     }  
  144.        
  145.         /**    
  146.          * 裁剪图片方法实现    
  147.          *     
  148.          * @param uri    
  149.          */    
  150.         public void startPhotoZoom(Uri uri) {    
  151.             if(uri==null){  
  152.                 Log.i("tag""The uri is not exist.");  
  153.             }  
  154.             Intent intent = new Intent("com.android.camera.action.CROP");    
  155.             intent.setDataAndType(uri, "image/*");    
  156.             // 设置裁剪    
  157.             intent.putExtra("crop""true");    
  158.             // aspectX aspectY 是宽高的比例    
  159.             intent.putExtra("aspectX"1);    
  160.             intent.putExtra("aspectY"1);    
  161.             // outputX outputY 是裁剪图片宽高    
  162.             intent.putExtra("outputX"320);    
  163.             intent.putExtra("outputY"320);    
  164.             intent.putExtra("return-data"true);    
  165.             startActivityForResult(intent, 2);    
  166.         }    
  167.        
  168.         /**    
  169.          * 保存裁剪之后的图片数据    
  170.          *     
  171.          * @param picdata    
  172.          */    
  173.         private void setImageToView(Intent data) {    
  174.             Bundle extras = data.getExtras();    
  175.             if (extras != null) {    
  176.                 Bitmap photo = extras.getParcelable("data");    
  177.                 Drawable drawable = new BitmapDrawable(photo);    
  178.                 btn_crop.setBackgroundDrawable(drawable);  
  179.             }    
  180.         }    
  181.       
  182.       
  183. }
0 0
原创粉丝点击