8

How to use default camera to take a picture in android ?

Sawan Modi
  • 227
  • 2
  • 5
  • 13
  • possible duplicate of [Android camera intent.](http://stackoverflow.com/questions/2729267/android-camera-intent) – Aleadam May 10 '11 at 05:27

3 Answers3

7
Uri imageUri;
final int TAKE_PICTURE = 115;

public void capturePhoto(View view) {
    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
    File photoFile = new File(Environment.getExternalStorageDirectory(),  "Photo.png");
    intent.putExtra(MediaStore.EXTRA_OUTPUT,
            Uri.fromFile(photoFile));
    imageUri = Uri.fromFile(photoFile);
    startActivityForResult(intent, TAKE_PICTURE);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
        case TAKE_PICTURE:
            if (resultCode == Activity.RESULT_OK) {
                Uri selectedImageUri = imageUri;
                //Do what ever you want
        }
    }
}
  • 1
    Thank you so much, I've been fumbling around for like an hour trying to get the camera object and preview method to work, when this is all I wanted from the beginning. – Dan F May 23 '11 at 19:33
  • Hi Abhan, your code will show all 3rd party camera apps as well as default camera app. What if some one want to show only default camera without showing any chooser of 3rd party camera apps like **Line Camera** and **Paper Camera**? – Nauman Zubair Sep 27 '13 at 12:16
  • @NaumanZubair It is hard to set. We have method named `SetClassName` but for that you would to know the exact class of the default camera app but as we all know that we have different `OEMs` and it may happen they use different packages for default app. So this is kind of trial and error scenario. –  Sep 29 '13 at 06:38
  • i found that solution, here it is http://stackoverflow.com/questions/19050810/how-to-open-default-camera-without-showing-chooser – Nauman Zubair Sep 30 '13 at 09:03
  • @NaumanZubair I do not think so this is the solution. As you can say that this is a workaround to fulfill particular requirement. In any application we always need to think about the `User`. So leave up to the `User` what they want to choose. –  Sep 30 '13 at 14:12
  • 1
    @Abhan I understand your point, but in case of specific users and according to clients' restricted requirements it may helpful for other developers. – Nauman Zubair Sep 30 '13 at 16:09
7

The intent which is used to open the camera is

 buttonCapturePhoto.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent, CAPTURE_IMAGE);
        }
    });

The code which gives you the image after capturing is

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Uri uriImage;
    InputStream inputStream = null;
    if ( (requestCode == SELECT_IMAGE || requestCode == CAPTURE_IMAGE) && resultCode == Activity.RESULT_OK) {
        uriImage = data.getData();
        try {
            inputStream = getContentResolver().openInputStream(uriImage);
            Bitmap bitmap = BitmapFactory.decodeStream(inputStream, null, options);
            imageView.setImageBitmap(bitmap);
        } catch (NullPointerException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        imageView.setAdjustViewBounds(true);
    }
}

Vineet
  • 99
  • 2
0

This is a simple example.Anyway this will return the image as a small bitmap.If you want to retrive the full-sized image ,is a bit more complicated.

ImageView  takePhotoView = (ImageView) findViewById(R.id.iwTakePicture);
Bitmap imageBitmap = null;
takePhotoView.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub              
                dispatchTakePictureIntent(0);
            }
        });

    private void dispatchTakePictureIntent(int actionCode) {
          Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);     
          startActivityForResult(takePictureIntent, actionCode);      
        }

    private void handleSmallCameraPhoto(Intent intent) {
        Bundle extras = intent.getExtras();
        this.imageBitmap = (Bitmap) extras.get("data");
        takePhotoView.setImageBitmap(imageBitmap);
    }

   @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
        if(resultCode == RESULT_OK)
            handleSmallCameraPhoto(data);
   }
Roman Marius
  • 456
  • 3
  • 9
  • 21