捕获照片意图resultCode

时间:2017-08-01 09:42:14

标签: java android

我如何检测到用户拍照并选择它(使用相机上的确认按钮)或者他们只是打开相机,拍照并将其删除(使用相机上的取消按钮)

当用户拍照时,我将该图片加载到ImageView中。如果用户点击确认按钮,则一切正常,但如果用户不想要该图片并决定点击取消按钮,则ImageView将变为空白。

这是我的相机意图:

void capturePhoto() {
//        ImagePicker.pickImage(this, "Select your image:");

        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        // Ensure that there's a camera activity to handle the intent
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            // Create the File where the photo should go
            File f = null;

            try {
                f = setUpPhotoFile();
                mCurrentPhotoPath = f.getAbsolutePath();
                Uri photoURI = Uri.fromFile(f);
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            } catch (IOException e) {
                e.printStackTrace();
                f = null;
                mCurrentPhotoPath = null;
                pictureUri = null;
            }

            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
        }
    }

和onActivityResult,在两种情况下,resultCode总是1.(注意RESULT_OK是-1),我不知道为什么。

这是我使用Glide将image设置为ImageView的方式:

Glide.with(this).load(mCurrentPhotoPath).centerCrop().into(imageView);

任何建议?

谢谢!

2 个答案:

答案 0 :(得分:0)

你只需要在onActivityresult中传递if语句 如果您直接使用该URI或您使用的任何没有任何框架设置的原因导致用户取消它,所以只需按照给定的方式执行

 //if needed than
//public static final int RESULT_OK = -1;
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);


    if (resultCode == RESULT_OK) {
        switch (requestCode) {

        case REQUEST_TAKE_PHOTO:

                   //do your stuff here
        }
    }

答案 1 :(得分:0)

您可以使用以下代码

 static final int REQUEST_IMAGE_CAPTURE = 1;

    private void dispatchTakePictureIntent() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            if (isCameraPermissionEnabled()) {
                startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
            }else {
                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.CAMERA},
                        1);
            }
        }
    }

    public boolean isCameraPermissionEnabled() {

        return !(Build.VERSION.SDK_INT >= 23 &&
                ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED );
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
            Bundle extras = data.getExtras();
            mBitmap = (Bitmap) extras.get("data");
            imageView.setBackground(new BitmapDrawable(getResources(),mBitmap));
        }
    }

//了解更多信息https://developer.android.com/training/camera/photobasics.html