Japz
Japz

Reputation: 93

How to capture picture using front camera on background step by step?

How to capture picture on front camera, I am working on sms event when my phone received a certain sms with code my phone will automatically capture picture and save it to sd card.

please help me step by step on capturing picture and save to sd card.

Upvotes: 4

Views: 3249

Answers (1)

Swayam
Swayam

Reputation: 16354

Use the following code to take a picture :

                Intent intent = new Intent();
                intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
                String newPicFile = "Image" + position;
                String outPath = "/sdcard/" + newPicFile;
                File outFile = new File(outPath);
                mCameraFileName = outFile.toString();
                Uri outuri = Uri.fromFile(outFile);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, outuri);
                try{
                    startActivityForResult(intent, 1);
                }
                catch (ActivityNotFoundException e) 
                {
                    Toast.makeText(context, "No Camera Found", Toast.LENGTH_LONG).show();
                }

And this code to view the clicked picture :

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
        if( requestCode == CAMERA_PIC_REQUEST)
        {
        //  data.getExtras()
            Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
            ImageView image =(ImageView) findViewById(R.id.PhotoCaptured);
            image.setImageBitmap(thumbnail);
        }
        else 
        {
            Toast.makeText(context, "Picture Not taken", Toast.LENGTH_LONG).show();
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

To check for the front camera in your device, you can make use of the following code :

CameraInfo cameraInfo = new CameraInfo();
            for (int i = 0; i < numberOfCameras; i++) {
                Camera.getCameraInfo(i, cameraInfo);
                if (cameraInfo.facing == CameraInfo.CAMERA_FACING_BACK) {
                    defaultCameraId = i;
                }
            }

Also, have a look at this documentation.

Upvotes: 2

Related Questions