Sivakumar S
Sivakumar S

Reputation: 129

How to display front camera by default in android studio

When clicking on a button I need to open the front camera by default but back camera is displaying. Please give me some idea how to open front camera.

This is my code:

b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra("android.intent.extras.CAMERA_FACING", 1);
            startActivityForResult(intent, 0);
        }
    }); Bitmap bmp;
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);

    if ( data != null && data.getExtras() !=  null) {
        bmp = (Bitmap) data.getExtras().get("data");
        iv.setImageBitmap(bmp);
    }

}

Upvotes: 0

Views: 10430

Answers (1)

Soham
Soham

Reputation: 4417

This below code should work(without intent) .Refer from this

 Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
       int 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());
                }
            }
        }

If you want to use intent check this link .I think that this solution may not work for all phones.Check the official doc for more reference.I suggest that you should not care what camera is used. It is up to the third-party camera app that you are invoking.

Upvotes: 1

Related Questions