在android的开发过程中,经常遇到设置用户头像以及裁剪图像大小的功能。昨天我遇到了设置用户头像的功能,开始不知道怎么搞,在技术群里问也没人回答,就研究了微信、以及用户设置头像的功能,了解到用户设置图像的过程,就是两步点击设置头像,选择头像资源的来源,相册和相机两个来得到。
闲话说的有点多了,来说说技术点吧。其实也比较简单,就是通过Intent来启动相册或者相机就可以了。
看下效果图:
启动相册以及相机关键代码:
- switch (which) {
- case 0:
-
- Intent intentFromGallery = new Intent();
- intentFromGallery.setType("image/*");
- intentFromGallery
- .setAction(Intent.ACTION_GET_CONTENT);
- startActivityForResult(intentFromGallery,
- IMAGE_REQUEST_CODE);
- break;
- case 1:
-
- Intent intentFromCapture = new Intent(
- MediaStore.ACTION_IMAGE_CAPTURE);
-
- if (Tools.hasSdcard()) {
-
- intentFromCapture.putExtra(
- MediaStore.EXTRA_OUTPUT,
- Uri.fromFile(new File(Environment
- .getExternalStorageDirectory(),
- IMAGE_FILE_NAME)));
- }
-
- startActivityForResult(intentFromCapture,
- CAMERA_REQUEST_CODE);
- break;
- }
打开系统自带的裁剪功能代码:
-
-
-
-
-
- public void startPhotoZoom(Uri uri) {
-
- Intent intent = new Intent("com.android.camera.action.CROP");
- intent.setDataAndType(uri, "image/*");
-
- intent.putExtra("crop", "true");
-
- intent.putExtra("aspectX", 1);
- intent.putExtra("aspectY", 1);
-
- intent.putExtra("outputX", 320);
- intent.putExtra("outputY", 320);
- intent.putExtra("return-data", true);
- startActivityForResult(intent, 2);
- }
完整实现代码如下:
- package com.xzw.picture;
-
- import java.io.File;
-
- import com.xzw.utils.Tools;
-
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.graphics.Bitmap;
- import android.graphics.drawable.BitmapDrawable;
- import android.graphics.drawable.Drawable;
- import android.net.Uri;
- import android.os.Bundle;
- import android.os.Environment;
- import android.provider.MediaStore;
- import android.view.View;
- import android.view.Window;
- import android.widget.ImageView;
- import android.widget.RelativeLayout;
- import android.widget.Toast;
-
-
-
-
-
-
- public class MainActivity extends Activity {
-
- private RelativeLayout switchAvatar;
- private ImageView faceImage;
-
- private String[] items = new String[] { "选择本地图片", "拍照" };
-
- private static final String IMAGE_FILE_NAME = "faceImage.jpg";
-
-
- private static final int IMAGE_REQUEST_CODE = 0;
- private static final int CAMERA_REQUEST_CODE = 1;
- private static final int RESULT_REQUEST_CODE = 2;
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- setContentView(R.layout.main);
- switchAvatar = (RelativeLayout) findViewById(R.id.switch_face_rl);
- faceImage = (ImageView) findViewById(R.id.face);
-
- switchAvatar.setOnClickListener(listener);
- }
-
- private View.OnClickListener listener = new View.OnClickListener() {
-
- @Override
- public void onClick(View v) {
- showDialog();
- }
- };
-
-
-
- private void showDialog() {
-
- new AlertDialog.Builder(this)
- .setTitle("设置头像")
- .setItems(items, new DialogInterface.OnClickListener() {
-
- @Override
- public void onClick(DialogInterface dialog, int which) {
- switch (which) {
- case 0:
- Intent intentFromGallery = new Intent();
- intentFromGallery.setType("image/*");
- intentFromGallery
- .setAction(Intent.ACTION_GET_CONTENT);
- startActivityForResult(intentFromGallery,
- IMAGE_REQUEST_CODE);
- break;
- case 1:
-
- Intent intentFromCapture = new Intent(
- MediaStore.ACTION_IMAGE_CAPTURE);
-
- if (Tools.hasSdcard()) {
-
- intentFromCapture.putExtra(
- MediaStore.EXTRA_OUTPUT,
- Uri.fromFile(new File(Environment
- .getExternalStorageDirectory(),
- IMAGE_FILE_NAME)));
- }
-
- startActivityForResult(intentFromCapture,
- CAMERA_REQUEST_CODE);
- break;
- }
- }
- })
- .setNegativeButton("取消", new DialogInterface.OnClickListener() {
-
- @Override
- public void onClick(DialogInterface dialog, int which) {
- dialog.dismiss();
- }
- }).show();
-
- }
-
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- //结果码不等于取消时候 if (resultCode != RESULT_CANCELED) {
- switch (requestCode) {
- case IMAGE_REQUEST_CODE:
- startPhotoZoom(data.getData());
- break;
- case CAMERA_REQUEST_CODE:
- if (Tools.hasSdcard()) {
- File tempFile = new File(
- Environment.getExternalStorageDirectory()
- + IMAGE_FILE_NAME);
- startPhotoZoom(Uri.fromFile(tempFile));
- } else {
- Toast.makeText(MainActivity.this, "未找到存储卡,无法存储照片!",
- Toast.LENGTH_LONG).show();
- }
-
- break;
- case RESULT_REQUEST_CODE:
- if (data != null) {
- getImageToView(data);
- }
- break;
- }
- }
- super.onActivityResult(requestCode, resultCode, data);
- }
-
- /**
- * 裁剪图片方法实现
- *
- * @param uri
- */
- public void startPhotoZoom(Uri uri) {
-
- Intent intent = new Intent("com.android.camera.action.CROP");
- intent.setDataAndType(uri, "image/*");
-
- intent.putExtra("crop", "true");
-
- intent.putExtra("aspectX", 1);
- intent.putExtra("aspectY", 1);
-
- intent.putExtra("outputX", 320);
- intent.putExtra("outputY", 320);
- intent.putExtra("return-data", true);
- startActivityForResult(intent, 2);
- }
-
- /**
- * 保存裁剪之后的图片数据
- *
- * @param picdata
- */
- private void getImageToView(Intent data) {
- Bundle extras = data.getExtras();
- if (extras != null) {
- Bitmap photo = extras.getParcelable("data");
- Drawable drawable = new BitmapDrawable(photo);
- faceImage.setImageDrawable(drawable);
- }
- }
-
- }
还有一个工具类:
- package com.xzw.utils;
-
- import android.os.Environment;
-
-
-
-
-
- public class Tools {
-
-
-
-
- public static boolean hasSdcard(){
- String state = Environment.getExternalStorageState();
- if(state.equals(Environment.MEDIA_MOUNTED)){
- return true;
- }else{
- return false;
- }
- }
- }
以上就是实现的代码!完整的资源包。
本文转自xuzw13 51CTO博客,原文链接:http://blog.51cto.com/xuzhiwei/965511,如需转载请自行联系原作者