通过拍照或选择本地图片并剪裁图片设置成头像,并保存裁剪后图片与本地方便注册头像上传
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 private static final String SAVE_PIC_PATH=Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) ? Environment.getExtern alStorageDirectory().getAbsolutePath():"/mnt/sdcard";//保存到SD卡 25 26 27 /*头像名称*/ 28 private static final String IMAGE_FILE_NAME = "faceImage.jpg"; 29 30 /* 请求码*/ 31 private static final int IMAGE_REQUEST_CODE = 0; 32 private static final int CAMERA_REQUEST_CODE = 1; 33 private static final int RESULT_REQUEST_CODE = 2; 34 35 private String filtPath = ""; 36 37 @Override 38 public void onCreate(Bundle savedInstanceState) { 39 super.onCreate(savedInstanceState); 40 setContentView(R.layout.activity_crop); 41 context=this; 42 initViews(); 43 } 44 45 46 @Override 47 protected void onResume() { 48 // TODO Auto-generated method stub 49 super.onResume(); 50 initValues(); 51 } 52 53 @Override 54 protected void updateViews(Object o) { 55 // TODO Auto-generated method stub 56 57 } 58 59 @Override 60 protected void initViews() { 61 // TODO Auto-generated method stub 62 63 btn_crop=(Button)findViewById(R.id.btn_crop); 64 btn_crop.setOnClickListener(new View.OnClickListener() { 65 66 @Override 67 public void onClick(View v) { 68 // TODO Auto-generated method stub 69 showDialog(); 70 } 71 }); 72 73 } 74 75 @Override 76 protected void initValues() { 77 // TODO Auto-generated method stub 78 } 79 80 @Override 81 protected void initHandler() { 82 // TODO Auto-generated method stub 83 84 } 85 86 private void showDialog() { 87 88 new AlertDialog.Builder(this) 89 .setTitle("设置头像") 90 .setItems(items, new DialogInterface.OnClickListener() { 91 92 @Override 93 public void onClick(DialogInterface dialog, int which) { 94 switch (which) { 95 case 0: 96 Intent intentFromGallery = new Intent(); 97 intentFromGallery.setType("image/*"); // 设置文件类型 98 intentFromGallery.setAction(Intent.ACTION_GET_CONTENT); 99 startActivityForResult(intentFromGallery,IMAGE_REQUEST_CODE); 100 break; 101 case 1: 102 Intent intentFromCapture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 103 // 判断存储卡是否可以用,可用进行存储 104 if (hasSdcard()) { 105 intentFromCapture.putExtra(MediaStore.EXTRA_OUTPUT, 106 Uri.fromFile(new File(Environment.getExternalStorageDirectory(),IMAGE_FILE_NAME))); 107 } 108 startActivityForResult(intentFromCapture,CAMERA_REQUEST_CODE); 109 break; 110 } 111 } 112 }) 113 .setNegativeButton("取消", new DialogInterface.OnClickListener() { 114 115 @Override 116 public void onClick(DialogInterface dialog, int which) { 117 dialog.dismiss(); 118 } 119 }).show(); 120 121 } 122 123 @Override 124 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 125 // 结果码不等于取消时候 126 if (resultCode != RESULT_CANCELED) { 127 switch (requestCode) { 128 case IMAGE_REQUEST_CODE: 129 startPhotoZoom(data.getData()); 130 break; 131 case CAMERA_REQUEST_CODE: 132 if (hasSdcard()) { 133 File tempFile = new File(Environment.getExternalStorageDirectory(),IMAGE_FILE_NAME); 134 startPhotoZoom(Uri.fromFile(tempFile)); 135 } else { 136 showToast("未找到存储卡,无法存储照片!"); 137 } 138 139 break; 140 case RESULT_REQUEST_CODE: 141 if (data != null) { 142 setImageToView(data); 143 } 144 break; 145 } 146 } 147 super.onActivityResult(requestCode, resultCode, data); 148 } 149 150 /** 151 * 裁剪图片方法实现 152 * 153 * @param uri 154 */ 155 public void startPhotoZoom(Uri uri) { 156 if(uri==null){ 157 Log.i("tag", "The uri is not exist."); 158 } 159 Intent intent = new Intent("com.android.camera.action.CROP"); 160 intent.setDataAndType(uri, "image/*"); 161 // 设置裁剪 162 intent.putExtra("crop", "true"); 163 // aspectX aspectY 是宽高的比例 164 intent.putExtra("aspectX", 1); 165 intent.putExtra("aspectY", 1); 166 // outputX outputY 是裁剪图片宽高 167 intent.putExtra("outputX", 320); 168 intent.putExtra("outputY", 320); 169 intent.putExtra("return-data", true); 170 startActivityForResult(intent, 2); 171 } 172 173 /** 174 * 保存裁剪之后的图片数据 175 * 176 * @param picdata 177 */ 178 private void setImageToView(Intent data) { 179 Bundle extras = data.getExtras(); 180 if (extras != null) { 181 Bitmap photo = extras.getParcelable("data"); 182 Drawable drawable = new BitmapDrawable(photo); 183 184 Calendar calendar = Calendar.getInstance(); 185 String imgname = String.valueOf(calendar.getTimeInMillis()); 186 filtPath = saveBitmapToSDCard(photo, imgname); 187 btn_crop.setBackgroundDrawable(drawable); 188 } 189 } 190 191 192 193 public String saveBitmapToSDCard(Bitmap bitmap,String imagename) 194 { 195 FileOutputStream fos = null; 196 try { 197 String filePath = SAVE_PIC_PATH+ "/beauty/images/img-"+imagename + ".png"; 198 File file = new File(filePath); 199 if(!file.exists()) 200 { 201 file.getParentFile().mkdirs(); 202 file.createNewFile(); 203 } 204 fos = new FileOutputStream(file); 205 if (fos != null) { 206 bitmap.compress(Bitmap.CompressFormat.PNG, 90, fos); 207 fos.close(); 208 209 return filePath; 210 } 211 } catch (Exception e) { 212 e.printStackTrace(); 213 return null; 214 } 215 return null; 216 } 217 218 219 220 221 222 223 }