如何检测照片是否拍摄(相机意图)

时间:2015-10-14 18:17:29

标签: android android-intent android-camera android-camera-intent

我想发送相机意图拍摄的照片。

  • 相机正常
  • 我有来自mMediaUri.getPath()的路径(它是正确的)
  • 我有发送它的方法(postImage())(工作正常)

当我启动一个Intent时,相机正在显示,但方法postImage不会等到拍照。 PostImage只是在启动意图后加载。

拍照后如何加载postImage?

如何检测是否拍摄照片?

Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        mMediaUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
        if(mMediaUri == null){
          Toast.makeText(MainActivity.this, "Problem!", Toast.LENGTH_LONG).show();

        }
        else {
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mMediaUri);
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);

            postImage("mail", mMediaUri.getPath());

        }

    }

enter image description here

1 个答案:

答案 0 :(得分:6)

只需将其用于打开相机:

@api

用于检测捕获(确定或取消按钮)

static final int REQUEST_IMAGE_CAPTURE = 1;

private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
    }
}

不要忘记给予许可:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        Bundle extras = data.getExtras();
        Bitmap imageBitmap = (Bitmap) extras.get("data");
        mImageView.setImageBitmap(imageBitmap);
    }
}

另请查看以下链接:

http://developer.android.com/training/camera/photobasics.html https://developer.android.com/training/camera/index.html