Android 相机或相册选择图片裁剪

背景
这是一个我们经常使用的功能,比如上传头像、身份证,本功能可选择相机或相册图片后,进入裁剪页面,并可返回Bitmap格式的图片。

工具类SelectPicUtil

public class SelectPicUtil {

    public static final int PHOTO_REQUEST_CUT = 4;
    public static final int PHOTO_REQUEST_GALLERY = 2;
    public static final int PHOTO_REQUEST_CAMERA = 3;

    private static Uri mCutTempFile; //剪切图片后输出的路径
    private static File mCameraTempFile; //拍照后的临时文件

    public static void selectPic(final Activity activity) {
        if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            ToastUtil.showText(activity, activity.getString(R.string.no_card), ToastUtil.ONE_SECOND);
            return;
        }

        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setItems(R.array.img_select, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                switch (which) {
                    case 0:
                        openCamera(activity);
                        break;
                    case 1:
                        openGallery(activity);
                        break;
                }
            }
        });
        AlertDialog dialog = builder.create();
        dialog.show();
    }

    /*
   * 从相册获取
   */
    public static void openGallery(Activity activity) {
        Intent intent = new Intent(Intent.ACTION_PICK);
        intent.setType("image/*");
        activity.startActivityForResult(intent, PHOTO_REQUEST_GALLERY);
    }

    /*
     * 从相机获取
     */
    public static void openCamera(Activity activity) {
        mCameraTempFile = new File(Environment.getExternalStorageDirectory(), "photo_file");
        Uri uri = Uri.fromFile(mCameraTempFile);

        Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
        intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
        activity.startActivityForResult(intent, PHOTO_REQUEST_CAMERA);
    }

    /*
    * 剪切图片
    */
    public static void openCropActivity(Activity activity,Uri uri) {
        mCutTempFile = Uri.parse("file://" + "/" + Environment.getExternalStorageDirectory().getPath() + "/" + "small.jpg");

        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        intent.putExtra("crop", "true");
        intent.putExtra("outputFormat", "JPEG");// 图片格式
        intent.putExtra("noFaceDetection", true);// 取消人脸识别
        intent.putExtra(MediaStore.EXTRA_OUTPUT, mCutTempFile);
        intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());

        // 裁剪框的比例,1:1
        //intent.putExtra("aspectX", 1);
        //intent.putExtra("aspectY", 1);
        // 裁剪后输出图片的尺寸大小
        //intent.putExtra("outputX", 250);
        //intent.putExtra("outputY", 250);

        activity.startActivityForResult(intent, PHOTO_REQUEST_CUT);
    }

    public static Bitmap getAfterCropBitmap(Activity activity){
        File cameraTempFile2=SelectPicUtil.getCameraTempFile();
        if (cameraTempFile2 != null) {
            cameraTempFile2.delete();
        }

        Uri mCutTempFile=SelectPicUtil.getCutTempFile();
        Bitmap bitmap;
        try {
            bitmap = BitmapFactory.decodeStream(activity.getContentResolver().openInputStream(mCutTempFile));
        } catch (FileNotFoundException e) {
            return null;
        }
        return bitmap;
    }

    public static File getCameraTempFile() {
        return mCameraTempFile;
    }

    public static Uri getCutTempFile() {
        return mCutTempFile;
    }

}

Activity回调
调用此功能的页面必须实现以下功能。

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case SelectPicUtil.PHOTO_REQUEST_GALLERY:
                if (data != null)  SelectPicUtil.openCropActivity(this,data.getData());
                break;
            case SelectPicUtil.PHOTO_REQUEST_CAMERA:
                File cameraTempFile=SelectPicUtil.getCameraTempFile();
                if (cameraTempFile != null) SelectPicUtil.openCropActivity(this,Uri.fromFile(cameraTempFile));
                break;
            case SelectPicUtil.PHOTO_REQUEST_CUT:
                mImageView.setImageBitmap(getAfterCropBitmap(this));
                break;
        }
    }

添加权限


源码下载地址:地址

你可能感兴趣的:(Android 相机或相册选择图片裁剪)