1 package com.example.nanchen.cropimagetest; android.content.ComponentName; 4 import android.content.DialogInterface; 5 import android.content.DialogInterface.OnCancelListener; 6 import android.content.Intent; 7 import android.content.pm.ResolveInfo; 8 import android.graphics.Bitmap; 9 import android.graphics.Bitmap.CompressFormat; 10 import android.net.Uri; 11 import android.os.Bundle; 12 import android.os.Environment; 13 import android.provider.MediaStore; 14 import android.provider.MediaStore.Images.Media; 15 import android.support.v7.app.AlertDialog; 16 import android.support.v7.app.AlertDialog.Builder; 17 import android.support.v7.app.AppCompatActivity; 18 import android.view.View; 19 import android.widget.AdapterView; com.example.nanchen.cropimagetest.SelectDialog.SelectDialogListener; java.io.File; 24 import java.io.FileOutputStream; 25 import java.io.IOException; 26 import java.util.ArrayList; 27 import java.util.List; MainActivity extends AppCompatActivity { RoundImageView mHeadImage; 32 33 @Override onCreate(Bundle savedInstanceState) { 35 super.onCreate(savedInstanceState); 36 setContentView(R.layout.activity_main); 37 38 mHeadImage = (RoundImageView) findViewById(R.id.main_roundImage); 39 } PHOTO_PICKED_FROM_CAMERA = 1; PHOTO_PICKED_FROM_FILE = 2; CROP_FROM_CAMERA = 3; getIconFromPhoto(){ 46 Intent intent = new Intent(Intent.ACTION_GET_CONTENT, Media.EXTERNAL_CONTENT_URI); 47 intent.setType("image/*"); 48 startActivityForResult(intent, PHOTO_PICKED_FROM_FILE); 49 } selectPhoto() { 52 List<String> list = new ArrayList<>(); 53 list.add("拍照"); 54 list.add("相册"); 55 showDialog(new SelectDialogListener() { 56 @Override onItemClick(AdapterView<?> parent, View view, int position, long id) { 58 switch (position){ 59 case 0: 60 getIconFromCamera(); 61 break; 62 case 1: ; 65 default: 66 break; 67 } 68 } 69 },list); 70 71 } Uri imgUri; * 调用系统相机拍照 getIconFromCamera() { 79 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 80 imgUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), 81 "avatar_"+String.valueOf(System.currentTimeMillis())+".png")); 82 intent.putExtra(MediaStore.EXTRA_OUTPUT,imgUri); 83 startActivityForResult(intent,PHOTO_PICKED_FROM_CAMERA); 84 } SelectDialog showDialog(SelectDialogListener listener, List<String> list){ 87 SelectDialog dialog = new SelectDialog(this, 88 R.style.transparentFrameWindowStyle,listener,list); 89 dialog.show(); 90 return dialog; 91 } * 尝试裁剪图片 doCrop(){ 98 final ArrayList<CropOption> cropOptions = new ArrayList<>(); 99 final Intent intent = new Intent("com.android.camera.action.CROP"); 100 intent.setType("image/*"); 101 List<ResolveInfo> list = getPackageManager().queryIntentActivities(intent,0); 102 int size = list.size(); 103 if (size == 0){ 104 UIUtil.showToast(this,"当前不支持裁剪图片!"); 105 return; 106 } 107 intent.setData(imgUri); 108 intent.putExtra("outputX",300); 109 intent.putExtra("outputY",300); 110 intent.putExtra("aspectX",1); 111 intent.putExtra("aspectY",1); 112 intent.putExtra("scale",true); 113 intent.putExtra("return-data",true); (size == 1){ 117 Intent intent1 = new Intent(intent); 118 ResolveInfo res = list.get(0); 119 intent1.setComponent(new ComponentName(res.activityInfo.packageName,res.activityInfo.name)); 120 startActivityForResult(intent1,CROP_FROM_CAMERA); 121 }else { (ResolveInfo res : list) { 124 CropOption co = new CropOption(); 125 co.title = getPackageManager().getApplicationLabel(res.activityInfo.applicationInfo); 126 co.icon = getPackageManager().getApplicationIcon(res.activityInfo.applicationInfo); 127 co.appIntent = new Intent(intent); 128 co.appIntent.setComponent(new ComponentName(res.activityInfo.packageName,res.activityInfo.name)); 129 cropOptions.add(co); 130 } 131 132 CommonAdapter<CropOption> adapter = new CommonAdapter<CropOption>(this,cropOptions,R.layout.layout_crop_selector) { 133 @Override convert(ViewHolder holder, CropOption item) { 135 holder.setImageDrawable(R.id.iv_icon,item.icon); 136 holder.setText(R.id.tv_name,item.title); 137 } 138 }; 139 140 AlertDialog.Builder builder = new Builder(this); 141 builder.setTitle("choose a app"); 142 builder.setAdapter(adapter, new DialogInterface.OnClickListener() { 143 @Override onClick(DialogInterface dialog, int which) { 145 startActivityForResult(cropOptions.get(which).appIntent,CROP_FROM_CAMERA); 146 } 147 }); 148 builder.setOnCancelListener(new OnCancelListener() { 149 @Override onCancel(DialogInterface dialog) { 151 if (imgUri != null){ 152 getContentResolver().delete(imgUri,null,null); 153 imgUri = null; 154 } 155 } 156 }); 157 AlertDialog dialog = builder.create(); 158 dialog.show(); 159 160 } 161 } @Override onActivityResult(int requestCode, int resultCode, Intent data) { 166 super.onActivityResult(requestCode, resultCode, data); 167 if (resultCode != RESULT_OK){ 168 return; 169 } 170 switch (requestCode) { 171 case PHOTO_PICKED_FROM_CAMERA: 172 doCrop(); 173 break; 174 case PHOTO_PICKED_FROM_FILE: 175 imgUri = data.getData(); 176 doCrop(); 177 break; 178 case CROP_FROM_CAMERA: 179 if (data != null){ 180 setCropImg(data); 181 } 182 break; 183 default: 184 break; 185 } 186 } setCropImg(Intent picData){ 189 Bundle bundle = picData.getExtras(); 190 if (bundle != null){ 191 Bitmap mBitmap = bundle.getParcelable("data"); 192 mHeadImage.setImageBitmap(mBitmap); 193 saveBitmap(Environment.getExternalStorageDirectory() + "/crop_" 194 +System.currentTimeMillis() + ".png",mBitmap); 195 } 196 } saveBitmap(String fileName,Bitmap bitmap){ 199 File file = new File(fileName); 200 FileOutputStream fout = null; 201 try { 202 file.createNewFile(); 203 fout = new FileOutputStream(file); 204 bitmap.compress(CompressFormat.PNG,100,fout); 205 fout.flush(); 206 } catch (IOException e) { 207 e.printStackTrace(); 208 } finally { 209 try { 210 if (fout!=null){ 211 fout.close(); 212 } 213 UIUtil.showToast(MainActivity.this,"保存成功!"); 214 } catch (IOException e) { 215 e.printStackTrace(); 216 } 217 } 218 } btnClick(View view) { 221 selectPhoto(); 222 } 223 }
布局: