Skip to content Skip to sidebar Skip to footer

Default Camera To Take A Picture In Android

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

Solution 1:

The intent which is used to open the camera is

 buttonCapturePhoto.setOnClickListener(newOnClickListener() {
        @OverridepublicvoidonClick(View arg0) {
            Intentintent=newIntent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent, CAPTURE_IMAGE);
        }
    });

The code which gives you the image after capturing is

protectedvoidonActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Uri uriImage;
    InputStreaminputStream=null;
    if ( (requestCode == SELECT_IMAGE || requestCode == CAPTURE_IMAGE) && resultCode == Activity.RESULT_OK) {
        uriImage = data.getData();
        try {
            inputStream = getContentResolver().openInputStream(uriImage);
            Bitmapbitmap= 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);
    }
}

Solution 2:

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.

ImageViewtakePhotoView= (ImageView) findViewById(R.id.iwTakePicture);
BitmapimageBitmap=null;
takePhotoView.setOnClickListener(newView.OnClickListener() {

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

    privatevoiddispatchTakePictureIntent(int actionCode) {
          IntenttakePictureIntent=newIntent(MediaStore.ACTION_IMAGE_CAPTURE);     
          startActivityForResult(takePictureIntent, actionCode);      
        }

    privatevoidhandleSmallCameraPhoto(Intent intent) {
        Bundleextras= intent.getExtras();
        this.imageBitmap = (Bitmap) extras.get("data");
        takePhotoView.setImageBitmap(imageBitmap);
    }

   @OverrideprotectedvoidonActivityResult(int requestCode, int resultCode, Intent data) { 
        if(resultCode == RESULT_OK)
            handleSmallCameraPhoto(data);
   }

Post a Comment for "Default Camera To Take A Picture In Android"