You're getting this error because your call to Camera.open()
returns null
. This happend when the device doesn't have a back facing camera, as stating in the documentation for Camera.open().
Creates a new Camera object to access the first back-facing camera on the device. If the device does not have a back-facing camera, this returns null.
However, you could fix this by using:
camera = Camera.open(int);
Where the int is a Camera ID as returned by getNumberOfCameras(). You can find the details of a camera from its ID by using getCameraInfo().
However, in most cases you can access the front camera with the camera ID 0:
camera = Camera.open(0);
Additionally, if a device doesn't have a back facing camera, it is unlikely to have a flash light installed.
You can check the availability of a flash using:
boolean hasFlashLight = context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);