19

I need ONE function to check if front camera exist, and if so - show preview. I found a few links, but each of them talk about a specific device:

Can anyone provide a generic code for this? Or maybe a combined code that will work with all devices?

Community
  • 1
  • 1
OkyDokyman
  • 3,786
  • 7
  • 38
  • 50

4 Answers4

20

For APIs >=9, you can use the Camera class: http://developer.android.com/reference/android/hardware/Camera.html to see if it has more than one camera, and query the CameraInfo

  • getNumberOfCameras

  • getCameraInfo:

http://developer.android.com/reference/android/hardware/Camera.CameraInfo.html

Constants

int CAMERA_FACING_BACK The facing of the camera is opposite to that of the screen.

int CAMERA_FACING_FRONT The facing of the camera is the same as that of the screen.


For APIs >=5, an option is to read public List<Camera.Size> getSupportedPictureSizes (). Front facing cameras will usually have much lower max resolution than back cameras.

http://developer.android.com/reference/android/hardware/Camera.Parameters.html

Community
  • 1
  • 1
Aleadam
  • 40,203
  • 9
  • 86
  • 108
11

This was posted by kcoppock, but I figured someome will come across this message as well sometime. I tested it personally, worked great on Froyo.

PackageManager pm = getPackageManager();
boolean frontCam, rearCam;

//It would be safer to use the constant PackageManager.FEATURE_CAMERA_FRONT
//but since it is not defined for Android 2.2, I substituted the literal value
frontCam = pm.hasSystemFeature("android.hardware.camera.front");

rearCam = pm.hasSystemFeature(PackageManager.FEATURE_CAMERA);
Community
  • 1
  • 1
JustAGuy
  • 5,151
  • 11
  • 41
  • 55
  • Nice tip. You can also use a constant for the front camera instead of hard code the value. That is PackageManager.FEATURE_CAMERA_FRONT – androidevil Feb 16 '16 at 13:46
2

in 2.3 it is possible. http://developer.android.com/sdk/api_diff/9/changes/android.hardware.Camera.html

But before that, Android sdk did not have any generic api for finding and using the second front camera.

Aniket Awati
  • 1,381
  • 3
  • 13
  • 21
0

I think you can loop through the available cameras on the phone and check the facing camera info to find one (but it is only available sing API level 9)

this answer could help you: How do I open the "front camera" on the Android platform?

Community
  • 1
  • 1
BFil
  • 12,966
  • 3
  • 44
  • 48