Android 开发调用系统拍照和图片选取

1、图片选取

        Intent intent = new Intent(Intent.ACTION_PICK);
        intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
        startActivityForResult(intent, AVATAR_PICTURE);

2、选取图片回调

  @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case AVATAR_PICTURE://从图库选取的回调
                //filePath = data.getData().getPath();
                if (data != null) {//不为空,说明是图库选择回来的头像

                    Uri uri = data.getData();

                    if (uri == null) return;

                    Cursor c = getContentResolver().query(uri, new String[]{MediaStore.Images.Media.DATA}, null, null, null);
                    if (c == null) return;

                    boolean moveToNext = c.moveToNext();

                    if (moveToNext) {

                        mFilePath = c.getString(0);
                    }
                    c.close();
                    //取消操作,并没有选择图片
                } else {
                    return;
                }
                cropRawPhoto(Uri.fromFile(new File(mFilePath)), mIvAvatar, AVATAR_CROP);

                break;

            case AVATAR_CAMERA://拍照之后的回调
                if (resultCode == RESULT_OK) {
                    mFilePath = mCameraPath;
                    cropRawPhoto(Uri.fromFile(new File(mFilePath)), mIvAvatar, AVATAR_CROP);

                    //更新图库
                    Uri localUri = Uri.fromFile(new File(mFilePath));

                    Intent localIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, localUri);

                    sendBroadcast(localIntent);
                  //取消返回,
                } else if (requestCode == RESULT_CANCELED) {

                }
                break;                 
           case AVATAR_CROP://裁剪之后的回调
                if (data != null) {
                    Bundle extras = intent.getExtras();
                    if (extras != null) 
                    Bitmap photo = extras.getParcelable("data");
                } else {
                    return;
                }
                break;

3、调用拍照

  Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        // 判断存储卡是否可用,存储照片文件
        if (StorageUtils.hasSdcard()) {
            File file = new File(Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_PICTURES), System.currentTimeMillis() + ".jpg");
            mCameraPath = file.getAbsolutePath();
            Uri uri = Uri.fromFile(file);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
        }
        startActivityForResult(intent, AVATAR_CAMERA);

4、调用拍照之后的裁剪功能

**
     * 裁剪原始的图片
     */
    public void cropRawPhoto(Uri uri, CircleImageView ivAvatar, int CODE_RESULT_REQUEST) {

        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", ivAvatar.getWidth());
        intent.putExtra("outputY", ivAvatar.getHeight());
        intent.putExtra("return-data", true);

        startActivityForResult(intent, CODE_RESULT_REQUEST);
    }

你可能感兴趣的:(Android)