1

I am developing an app in which I want to open front camera on button click (if front camera is present on that device).

I have use this code which works for me on Asus Tablet having OS 3.2.1. But same code is not working on OS 4.2.1.

I want the code which works on all android versions. Please help me in this problem.

Following is my code-

Intent takePictureIntent = new Intent(
                MediaStore.ACTION_IMAGE_CAPTURE);

takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                            Uri.fromFile(f));
                    Log.v("", "Camera Id-" + camId);

                    if (!TextUtils.isEmpty(camera)) {
                        if (camera.equalsIgnoreCase("Front")) {
                            Log.v("", "Inside if");
                            takePictureIntent.putExtra(
                                    "android.intent.extras.CAMERA_FACING",
                                    Camera.CameraInfo.CAMERA_FACING_FRONT);

                        } else {
                            Log.v("", "Inside else");
                            takePictureIntent.putExtra(
                                    "android.intent.extras.CAMERA_FACING",
                                    Camera.CameraInfo.CAMERA_FACING_BACK);

                        }
                    }
startActivityForResult(takePictureIntent, actionCode);
Aniket Bhosale
  • 651
  • 2
  • 11
  • 14

2 Answers2

3

Note: This feature is available in Gingerbread and Up Android Version. Intent handles camera action in its own way. This technique is used when you are using SurfaceView to exploit camera functionality.

 private Camera openFrontFacingCameraGingerbread() {
int cameraCount = 0;
Camera cam = null;
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
cameraCount = Camera.getNumberOfCameras();
for ( int camIdx = 0; camIdx < cameraCount; camIdx++ ) {
    Camera.getCameraInfo( camIdx, cameraInfo );
    if ( cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT  ) {
        try {
            cam = Camera.open( camIdx );
        } catch (RuntimeException e) {
            Log.e(TAG, "Camera failed to open: " + e.getLocalizedMessage());
        }
    }
}

return cam;}

in manifest file:

<uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.hardware.camera" android:required="false" />  <uses-feature android:name="android.hardware.camera.front" android:required="false" />
Aarun
  • 1,002
  • 1
  • 8
  • 13
  • I have seen this answer in many posts(Hopefully all are posted by you).But am getting " Camera failed to open : Fail to connect to camera service" in a samsung device(android version 4.04). – Syamantak Basu Jan 10 '14 at 11:44
0

Today, I was trying the same thing and ran into same problems as yours. Then I searched the problem and found the perfect solution on this link which helped me. Android can't record video with Front Facing Camera, MediaRecorder start failed: -19

Community
  • 1
  • 1
Rohit
  • 510
  • 3
  • 15