4

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

klijakub
  • 845
  • 11
  • 31

1 Answers1

6

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

Oğuzhan Döngül
  • 7,856
  • 4
  • 38
  • 52
  • Great answer! Thank you! Do you have any idea is it possible to get rid of this OK button, and after photo is taken return OK? – klijakub Oct 14 '15 at 18:33
  • 1
    @klijakub This Screen(with Ok button) can be differ according to OS and Device model. For example, you do not see this screen on Sony XPERIA phones. If you don't want to see this screen, you need to create a custom camera activity. – Oğuzhan Döngül Oct 14 '15 at 18:37