Android 调用相机、打开相册、裁剪图片

    private ImageView iv_user_photo;
    private String fileName = "";
    private File tempFile;
    private int crop = 300;// 裁剪大小
    private static final int OPEN_CAMERA_CODE = 10;
    private static final int OPEN_GALLERY_CODE = 11;
    private static final int CROP_PHOTO_CODE = 12;


    private OnClickListener PopupWindowItemOnClick = new OnClickListener() {

        @Override
        public void onClick(View v) {
            menuWindow.dismiss();
            switch (v.getId()) {
            // 拍照
            case R.id.btn_camera:
                initFile();
                openCamera();
                break;
            // 相册
            case R.id.btn_gallery:
                initFile();
                openGallery();
                break;
            default:
                break;
            }
        }
    };

    public void initFile() {
        if(fileName.equals("")) {
            if(FileUtil.existSDCard()) {
                String path = Environment.getExternalStorageDirectory() + File.separator + "JanuBookingOnline" + File.separator;
                FileUtil.mkdir(path);
                Logger.i("path:" + path);
                fileName = path + "user_head_photo.jpg";
                tempFile = new File(fileName);
            } else {
                CommonUitl.toast(context, "请插入SD卡");
            }
        }
    }

    /**
     * 调用相机
     */
    public void openCamera() {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// 打开相机
        intent.putExtra("output", Uri.fromFile(tempFile));
        startActivityForResult(intent, OPEN_CAMERA_CODE);
    }

    /**
     * 打开相册
     */
    public void openGallery() {
        Intent intent = new Intent(Intent.ACTION_PICK);// 打开相册
        intent.setDataAndType(MediaStore.Images.Media.INTERNAL_CONTENT_URI, "image/*");
        intent.putExtra("output", Uri.fromFile(tempFile));
        startActivityForResult(intent, OPEN_GALLERY_CODE);
    }

    /**
     * 裁剪图片
     * @param uri
     */
    public void cropPhoto(Uri uri) {
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        intent.putExtra("output", Uri.fromFile(tempFile));
        intent.putExtra("crop", true);
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        intent.putExtra("outputX", crop);
        intent.putExtra("outputY", crop);
        startActivityForResult(intent, CROP_PHOTO_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == 1)
            return;

        switch (requestCode) {
        case OPEN_CAMERA_CODE:
            cropPhoto(Uri.fromFile(tempFile));
            break;
        case OPEN_GALLERY_CODE:
            cropPhoto(data.getData());
            break;
        case CROP_PHOTO_CODE:
            try {
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = 2;
                Bitmap bitmap = BitmapFactory.decodeFile(fileName, options);

                if (bitmap != null) {
                    iv_user_photo.setImageBitmap(bitmap);
                    CommonUitl.sharedPreferences(context, AppConstants.USER_PHOTO, fileName);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            break;
        default:
            break;
        }

        super.onActivityResult(requestCode, resultCode, data);
    }

原文地址:https://www.cnblogs.com/chaoyaximo/p/3307538.html