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

How to detect if photo was taken (camera intent)

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

当我启动一个 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());

        }

    }

简单地说,你可以用它来打开相机:

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);
    }
}

不要忘记授予权限:

<manifest ... >
    <uses-feature android:name="android.hardware.camera"
                  android:required="true" />
    ...
</manifest>

同时检查这些链接:

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