Jonathan Vukadinovic
Jonathan Vukadinovic

Reputation: 264

android deprecated camera class

I'm using the deprecated android camera object. I read here: https://source.android.com/devices/camera/versioning.html

that even though the camera object is deprecated it should still work for android 5.0+ version. I ask this, because I released an app that uses the deprecated camera object and my friends downloaded the app and it crashes. They reported the errors and I see in the trace stack that the camera can't even open. This app works on my phone and my phone is version 5.0+. So maybe I'm not opening the camera correctly??? Or maybe those phones can't use the deprecated class. Here's my code for opening the camera:

@Override
public void onResume() {
    super.onResume();
    if(!StopTouch)//ignore this condition not relevant to the problem
        StartUpCam();
}

private void StartUpCam()
{

        if(faceProc==null)//ignore this as well
            faceProc = FacialProcessing.getInstance();
        camera=Camera.open(getCid()); //crashes here!!! Calls getCid() which is defined below
        camera.setPreviewCallback(this);
        initPreview(width, height);
        startPreview();
        startTimer();
}

private int getCid()
{
    if(cameraFront && findFrontFacingCamera()>0)
        return findFrontFacingCamera();
    return findBackFacingCamera();
}

public static int findBackFacingCamera() {
    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_BACK) {
            cameraId = i;
            break;
        }
    }
    return cameraId;
}

public static int findFrontFacingCamera() {
    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) {
            cameraId = i;
            break;
        }
    }
    return cameraId;
}

Anyone else having issues with the deprecated camera class when they release their app? Any ideas what I'm doing wrong here??

Upvotes: 3

Views: 9381

Answers (2)

Shashank Udupa
Shashank Udupa

Reputation: 2223

If you tried to run it in Android Marshmallow you need to ask for permission dynamically rather than just declaring the Camera permission in manifest. Here's a list of permissions which need to be asked dynamically from marshmallow http://developer.android.com/guide/topics/security/permissions.html#normal-dangerous

Upvotes: 2

josedlujan
josedlujan

Reputation: 5600

If you want delete "deprecated" in your app, use camera2 check the documentation:

API: android.hardware.camera2

Upvotes: 0

Related Questions