Reputation: 5273
I want to take photo with front camara in my android app, i am tried this code but not not working all time opening back camara. My code is.
Intent callCameraApplicationIntent = new Intent();
callCameraApplicationIntent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);//camera application is called to capture
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException e) {
e.printStackTrace();
}
callCameraApplicationIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
callCameraApplicationIntent.putExtra("android.intent.extras.CAMERA_FACING", 1);
startActivityForResult(callCameraApplicationIntent, activityStartCameraApp);
Upvotes: 0
Views: 1213
Reputation: 3099
Try to use more feature permission in this way:
<!--Front camera-->
<uses-feature android:name="android.hardware.camera" android:required="false" />
<uses-feature android:name="android.hardware.camera.front" android:required="false" />
IMO, using front camera with Intent isn't very well documented also not working in most devices.
You can use camera preview class defined as a SurfaceView
that can display the live image data coming from a camera, so users can frame and capture a picture or video. The class implements SurfaceHolder.Callback
in order to capture the callback events for creating and destroying the view, which are needed for assigning the camera preview input.
private Camera openFrontCamera() {
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, "Failed to open: " + e.getLocalizedMessage());
}
}
}
return cam;
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
//Creates a Camera object to access a particular hardware camera.
// and throw a RuntimeException if camera is opened by other applications
mCamera =openFrontCamera();
try {
mCamera.setPreviewDisplay(surfaceView.getHolder());
} catch (Exception e) {
e.printStackTrace();
}
Upvotes: 0
Reputation: 9369
Taken from Google Camera's shortcut for Android 7.1 (but should work with older Androids)
intent.putExtra("android.intent.extra.USE_FRONT_CAMERA", true);
So, combined with previous answers, this works for me on all phones I could've test it on
intent.putExtra("android.intent.extras.CAMERA_FACING", android.hardware.Camera.CameraInfo.CAMERA_FACING_FRONT);
intent.putExtra("android.intent.extras.LENS_FACING_FRONT", 1);
intent.putExtra("android.intent.extra.USE_FRONT_CAMERA", true);
Upvotes: 1