Search code examples
androidandroid-intentandroid-cameraandroid-camera-intent

How to detect if photo was taken (camera intent)


I want to send a photo taken by camera intent.

  • Camera works fine
  • I have path from mMediaUri.getPath() (it's correct)
  • I have method to send it (postImage()) (works fine)

When I start an Intent, camera is showing up, but method postImage is not waiting until photo is taken. PostImage just loading after starting intent.

How to load postImage after photo was taken?

or

How to detect if photo was taken?

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


Solution

  • Simply you can use that for opening camera:

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

    and for detecting capture(OK or Cancel button)

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

    Do not forget giving permission:

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

    Also check these links:

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