android 头像选择以及裁剪

一、布局申明

<ImageView
                android:id="@+id/head_image"
                android:layout_width="80dp"
                android:layout_height="80dp"
                android:layout_centerHorizontal="true"
                android:background="@drawable/default_avatar" />

 

二、Activity中代码

private ImageView headImageView;
    
private BitmapUtil bitmapUtil = new BitmapUtil(this);


headImageView = (ImageView) findViewById(R.id.head_image);
        
        headImageView.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View v) {
                
                new android.app.AlertDialog.Builder(RegisterActivity.this)
                    .setTitle("头像选择")
                    .setNegativeButton("相册选取",
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.cancel();
                                bitmapUtil.doCropPhoto(RegisterActivity.this);
                            }
                        })
                    .setPositiveButton("相机拍照",
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.cancel();
                                String status = Environment.getExternalStorageState();
                                if (status.equals(Environment.MEDIA_MOUNTED)) {// 判断是否有SD卡
                                    bitmapUtil.doTakePhoto(RegisterActivity.this);// 用户点击了从照相机获取
                                }
                            }
                        }).show();
            }
        });

 

三、Activity回调方法

/**
     * 头像选择回调
     * resultCode:  正常返回-1   用户后退返回0
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.i("TAG", requestCode + " : " + resultCode);
        switch (requestCode) {
        case BitmapUtil.activity_result_camara_with_data: // 拍照
            try {
                if(resultCode == -1) {
                    bitmapUtil.cropImageUriByTakePhoto(RegisterActivity.this);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            break;
        case BitmapUtil.activity_result_cropimage_with_data:
            try {
                if(resultCode == -1) {
                    Bitmap bitmap = bitmapUtil.decodeUriAsBitmap(RegisterActivity.this);
                    if(bitmap != null) {
                        headImageView.setImageBitmap(bitmap);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            break;
        }
    }

 

四、工具类

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.os.SystemClock;
import android.provider.MediaStore;
import android.util.Log;

public class BitmapUtil {

    //对外公开的
    public Uri photoUri;
    
    private File picFile;
    private Uri tempUri;

    public final static int activity_result_camara_with_data = 1006;
    public final static int activity_result_cropimage_with_data = 1007;

    private Context context;
    
    public BitmapUtil(Context context) {
        super();
        this.context = context;
    }

    /**
     * 拍照获取图片
     * 
     */
    public void doTakePhoto() {
        try {
            File uploadFileDir = new File(Environment.getExternalStorageDirectory(), "/upload");
            Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            if (!uploadFileDir.exists()) {
                uploadFileDir.mkdirs();
            }
            picFile = new File(uploadFileDir, SystemClock.currentThreadTimeMillis() + ".png");
            if (!picFile.exists()) {
                picFile.createNewFile();
            }
            tempUri = Uri.fromFile(picFile);
            
            cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, tempUri);
            ((Activity) context).startActivityForResult(cameraIntent,
                    activity_result_camara_with_data);
            Log.i("TAG", "photo1:" + tempUri.toString() + ",photo2:" + tempUri.getPath());
        } catch (ActivityNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void doCropPhoto() {
        try {

            File pictureFileDir = new File(Environment.getExternalStorageDirectory(), "/upload");
            if (!pictureFileDir.exists()) {
                pictureFileDir.mkdirs();
            }
            picFile = new File(pictureFileDir, SystemClock.currentThreadTimeMillis() + ".png");
            if (!picFile.exists()) {
                picFile.createNewFile();
            }
            tempUri = Uri.fromFile(picFile);
            
            final Intent intent = getCropImageIntent();
            
            ((Activity) context).startActivityForResult(intent,
                    activity_result_cropimage_with_data);
            Log.i("TAG", "photo3:" + tempUri.toString() + ",photo4:"+ tempUri.getPath());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Constructs an intent for image cropping. 调用图片剪辑程序
     */
    private Intent getCropImageIntent() {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
        intent.setType("image/*");
        intent.putExtra("crop", "true");
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        intent.putExtra("outputX", 100);
        intent.putExtra("outputY", 100);
        intent.putExtra("noFaceDetection", true);
        intent.putExtra("scale", true);
        intent.putExtra("return-data", false);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, tempUri);
        intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
        return intent;
    }

    public void cropImageUriByTakePhoto() {
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(tempUri, "image/*");
        intent.putExtra("crop", "true");
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        intent.putExtra("outputX", 100);
        intent.putExtra("outputY", 100);
        intent.putExtra("scale", true);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, tempUri);
        intent.putExtra("return-data", false);
        intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
        intent.putExtra("noFaceDetection", true); // no face detection
        ((Activity) context).startActivityForResult(intent,
                activity_result_cropimage_with_data);
    }

    public Bitmap decodeUriAsBitmap() {
        Bitmap bitmap = null;
        try {
            if(tempUri != null) {
                photoUri = tempUri;
                bitmap = BitmapFactory.decodeStream(context.getContentResolver().openInputStream(photoUri));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        }
        return bitmap;
    }
}

 

android 头像选择以及裁剪,,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。