4

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.

Japz
  • 93
  • 2
  • 9

1 Answers1

2

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.

Swayam
  • 16,294
  • 14
  • 64
  • 102
  • sir swayam how do i call the capturing picture? sorry im a beginner. – Japz Sep 13 '12 at 22:18
  • I am sorry, I dont understand. You want to view the picture that you have taken ? That code is there in my answer too. And, it's cool, no need to call me 'sir'. – Swayam Sep 14 '12 at 04:05
  • ahm well swayam for more detailed info on my question. I'm working an auto capturing picture on background without any clicking buttons, I want is I will set the time interval for every 5 minutes and then the phone will capture picture on front camera and save to sd card can you help me please..? I'm just a student. – Japz Sep 14 '12 at 14:35
  • Check the first part to my answer, where there is the code to take a picture. You just put this in a function and call it every 5 mins or so as you please. – Swayam Sep 14 '12 at 21:26
  • ahm the code was not auto capturing pix it open camera intent and it does not save any captured pix. again I need a button that captured pix and save to sd card without opening intent if the camera. – Japz Sep 15 '12 at 02:11
  • Hi I already got the answer using other methods please don't answer my question anymore thank you swayam. cheers ^J^ – Japz Sep 15 '12 at 15:26
  • Oh, okay! Great then! Good luck ahead! Cheers! :) – Swayam Sep 15 '12 at 20:36
  • @Swayam is it possible to capture image without preview (silent mode ) ? – eawedat Dec 25 '13 at 18:41
  • Could you please explain what you mean by "without preview" ? – Swayam Dec 26 '13 at 04:25
  • @Swayam i want to capture picture in background service ... is there any way to do it... no need to intent ... – chhameed Jan 16 '14 at 11:23
  • 1
    Oh.. Sorry I didn't understand what you meant at first. Yes, of course it is. Have a look at http://stackoverflow.com/questions/9744790/android-possible-to-camera-capture-without-a-preview – Swayam Jan 16 '14 at 11:41