2017-08-01 89 views
-1

如何檢測用戶拍照並選擇它(用相機上的確認按鈕)或者他們只需打開相機,拍照並將其取下(使用相機上的取消按鈕)捕獲照片意圖結果代碼

當用戶拍照時,我將該圖片加載到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),我不知道爲什麼。

這是我如何使用滑翔設置imageImageView

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

有什麼建議?

謝謝!

回答

0

你只需要,如果你使用if語句來傳遞onActivityresult 原因直接在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 
     } 
    } 
+0

的事情是我用String mCurrentPhotoPath送照片,而不是位圖。 resultCode是1,而不是RESULT_OK。我不知道爲什麼 –

+0

我忘記了result_ok檢查現在檢查 –

+0

其實你不必聲明,因爲它是默認值的活動,你必須先嚐試 –

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

+0

我從這個網站的代碼,但我保存全尺寸的照片。 https://developer.android.com/training/camera/photobasics.html#TaskPath –