Using Camera.TakePicture with Front Camera Causes Phone to Crash

纵饮孤独 提交于 2020-01-25 12:22:27

问题


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 attempting to take a picture causes the entire phone to crash.

The only way I've been able to "resolve" the issue has been by changing:

mCamera = Camera.open(1);

to:

mCamera = Camera.open();

This doesn't make sense to me as Camera.open(1) had been working just fine for the entire life of the app prior. The phone even crashes when using:

mCamera.takePicture(null, null, null); //Useless I know. Just for test purposes.

The worst part is that I can't get a logcat of the problem because the entire phone crashes and restarts. And if I attempt to run it in an emulator it runs just fine. And even with tons of searching I haven't found anyone that has had the camera api cause the entire phone to crash.

So I guess my question is, does anyone have an idea what might be causing this? Or where I might begin to look to fix it?

Edit: I should add that I also tried using:

int cameraCount = 0;
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 {
            mCamera = Camera.open(camIdx);
        } catch (RuntimeException e) {
            Log.e(TAG, "Camera failed to open: " + e.getLocalizedMessage());
        }
    }
}

This also opened the front facing camera and allowed me to preview, same as mCamera = Camera.open(1); but still crashes the phone when taking a picture.


回答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.

public int getCamera() 
{ 
    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; 
} 



回答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:

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

Hope it helps!

Regards!



来源:https://stackoverflow.com/questions/24916670/using-camera-takepicture-with-front-camera-causes-phone-to-crash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!