簡體   English   中英

捕獲照片意圖結果代碼

[英]Capture photo intent resultCode

如何檢測用戶拍照並選擇照片(使用相機上的確認按鈕),或者他們只是打開相機,拍照並刪除照片(使用相機上的取消按鈕)

當用戶拍照時,我正在將該圖片加載到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);

有什么建議么?

謝謝!

您只需要傳遞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
        }
    }

您可以使用以下代碼

 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM