Skip to content Skip to sidebar Skip to footer

Using Camera.takepicture With Front Camera Causes Phone To Crash

I'm working on a simple app that lets the user take a picture of their self and then make modifications to the image. This had been working splendidly but now all of a sudden attem

Solution 1:

First check whether your phone that camera id that you are passing. This is a helper method to get the id of back camera.

publicintgetCamera() 
{ 
    int numCameras = Camera.getNumberOfCameras(); 
    CameraInfo cameraInfo = new CameraInfo(); 
    for (int i = 0; i < numCameras; i++) 
    { 
        Camera.getCameraInfo(i, cameraInfo); 
        if (cameraInfo.facing == CameraInfo.CAMERA_FACING_BACK) 
        { 
            return i; 
        }
    } 
    return-1; 
} 

Solution 2:

Well, there's a major problem with the code, you are hardcoding a camera id, and that's a bomb that will explode at any time, in stead you must do the following:

You need to use http://developer.android.com/reference/android/hardware/Camera.html to see if it has more than one camera, and query the CameraInfo

getNumberOfCameras

getCameraInfo

and use 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.

Once you get the info of the camera you need, you must use whatever ID it provides to open your camera.

Something like this:

intcameraId= -1;
intnumberOfCameras= Camera.getNumberOfCameras();
for (inti=0; i < numberOfCameras; i++) {
  CameraInfoinfo=newCameraInfo();
  Camera.getCameraInfo(i, info);
  if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
    Log.d(DEBUG_TAG, "Camera found");
    cameraId = i;
    break;
  }
}

Hope it helps!

Regards!

Post a Comment for "Using Camera.takepicture With Front Camera Causes Phone To Crash"