布局 一个ImageView用来显示处理后的头像,两个按钮 分别为调用相机、调用手机相册
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_test_choose_touxiang" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="ht.testxyapp.com.testapp.activity.TestChooseTouxiang"> <ImageView android:id="@+id/imgTouXiang" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/circle_touxiang"/> <Button android:id="@+id/btnChooseFromCamera" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="拍照上传" /> <Button android:id="@+id/btnChooseFromXc" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="相册上传" /> </LinearLayout>
然后是主activity
public class TestChooseTouxiang extends AppCompatActivity { private Button btnChooseFromCamera, getBtnChooseFromXc; private ImageView imgTouXiang; /* 头像文件 */ private static final String IMAGE_FILE_NAME = "temp_head_image.jpg"; /* 请求识别码 */ private static final int CODE_GALLERY_REQUEST = 0xa0; private static final int CODE_CAMERA_REQUEST = 0xa1; private static final int CODE_RESULT_REQUEST = 0xa2; // 裁剪后图片的宽(X)和高(Y),480 X 480的正方形。 private static int output_X = 600; private static int output_Y = 600; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test_choose_touxiang); imgTouXiang = (ImageView) findViewById(R.id.imgTouXiang); btnChooseFromCamera = (Button) findViewById(R.id.btnChooseFromCamera); getBtnChooseFromXc = (Button) findViewById(R.id.btnChooseFromXc); btnChooseFromCamera.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { choseHeadImageFromCameraCapture(); } }); getBtnChooseFromXc.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { choseHeadImageFromGallery(); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent intent) { // 用户没有进行有效的设置操作,返回 if (resultCode == RESULT_CANCELED) { Toast.makeText(getApplication(), "取消", Toast.LENGTH_LONG).show(); return; } switch (requestCode) { case CODE_GALLERY_REQUEST: Toast.makeText(this, "code--1", Toast.LENGTH_SHORT).show(); cropRawPhoto(intent.getData()); break; case CODE_CAMERA_REQUEST: if (hasSdcard()) { File tempFile = new File( Environment.getExternalStorageDirectory(), IMAGE_FILE_NAME); String path = tempFile.getPath(); Toast.makeText(this, path+"89", Toast.LENGTH_SHORT).show(); cropRawPhoto(Uri.fromFile(tempFile)); } else { Toast.makeText(getApplication(), "没有SDCard!", Toast.LENGTH_LONG) .show(); } break; case CODE_RESULT_REQUEST: Toast.makeText(this, "code--3", Toast.LENGTH_SHORT).show(); if (intent != null) { setImageToHeadView(intent); } //Toast.makeText(this, "intent为null", Toast.LENGTH_SHORT).show(); break; } super.onActivityResult(requestCode, resultCode, intent); } /** * 裁剪原始的图片 */ public void cropRawPhoto(Uri uri) { Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, "image/*"); // 设置裁剪 intent.putExtra("crop", "true"); // aspectX , aspectY :宽高的比例 intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); // outputX , outputY : 裁剪图片宽高 intent.putExtra("outputX", output_X); intent.putExtra("outputY", output_Y); intent.putExtra("return-data", true); startActivityForResult(intent, CODE_RESULT_REQUEST); } //设置图片作为头像 private void setImageToHeadView(Intent intent) { PhotoUtil photoUtil = new PhotoUtil(); Bundle extras = intent.getExtras(); if (extras != null) { // Toast.makeText(this, extras.getParcelable("data")+"*", Toast.LENGTH_SHORT).show(); Bitmap photo = extras.getParcelable("data"); photo = photoUtil.toRoundBitmap(photo); imgTouXiang.setImageBitmap(photo); } } // 启动手机相机拍摄照片作为头像 private void choseHeadImageFromCameraCapture() { Intent intentFromCapture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // 判断存储卡是否可用,存储照片文件 if (hasSdcard()) { intentFromCapture.putExtra(MediaStore.EXTRA_OUTPUT, Uri .fromFile(new File(Environment .getExternalStorageDirectory(), IMAGE_FILE_NAME))); } startActivityForResult(intentFromCapture, CODE_CAMERA_REQUEST); } // 从本地相册选取图片作为头像 private void choseHeadImageFromGallery() { Intent intentFromGallery = new Intent(); // 设置文件类型 intentFromGallery.setType("image/*");//选择图片 intentFromGallery.setAction(Intent.ACTION_GET_CONTENT); //如果你想在Activity中得到新打开Activity关闭后返回的数据, //你需要使用系统提供的startActivityForResult(Intent intent,int requestCode)方法打开新的Activity startActivityForResult(intentFromGallery, CODE_GALLERY_REQUEST); } /** * 检查设备是否存在SDCard的工具方法 */ public static boolean hasSdcard() { String state = Environment.getExternalStorageState(); if (state.equals(Environment.MEDIA_MOUNTED)) { // 有存储的SDCard return true; } else { return false; } } }
其中设置图片作为头像方法中 有一个PhotoUtil 类 它主要用于将所选择裁剪后的图像变为圆形 设置
这个类为:
public class PhotoUtil { public Bitmap toRoundBitmap(Bitmap bitmap) { int width = bitmap.getWidth(); int height = bitmap.getHeight(); float roundPx; float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom; if (width <= height) { roundPx = width / 2; top = 0; bottom = width; left = 0; right = width; height = width; dst_left = 0; dst_top = 0; dst_right = width; dst_bottom = width; } else { roundPx = height / 2; float clip = (width - height) / 2; left = clip; right = width - clip; top = 0; bottom = height; width = height; dst_left = 0; dst_top = 0; dst_right = height; dst_bottom = height; } Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xff424242; final Paint paint = new Paint(); final Rect src = new Rect((int) left, (int) top, (int) right, (int) bottom); final Rect dst = new Rect((int) dst_left, (int) dst_top, (int) dst_right, (int) dst_bottom); final RectF rectF = new RectF(dst); paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawRoundRect(rectF, roundPx, roundPx, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(bitmap, src, dst, paint); return output; } }
好了,到此就实现了,权限需要加上存储、读取文件的权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
拿走即可使用!