android 弃用相机类

标签 android camera android-camera deprecated

我正在使用已弃用的 android 相机对象。我在这里读到: https://source.android.com/devices/camera/versioning.html

即使相机对象已被弃用,它仍然适用于 android 5.0+ 版本。我问这个,因为我发布了一个使用已弃用的相机对象的应用程序,我的 friend 下载了该应用程序并且它崩溃了。他们报告了错误,我在跟踪堆栈中看到相机甚至无法打开。这个应用程序适用于我的手机,我的手机是 5.0+ 版本。所以也许我没有正确打开相机???或者这些手机可能无法使用已弃用的类。这是我打开相机的代码:

@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;
}

还有其他人在发布他们的应用程序时遇到过已弃用的相机类的问题吗?知道我在这里做错了什么吗??

最佳答案

如果您尝试在 Android Marshmallow 中运行它,您需要动态请求权限,而不是仅仅在 list 中声明相机权限。 这是需要从棉花糖动态询问的权限列表 http://developer.android.com/guide/topics/security/permissions.html#normal-dangerous

关于android 弃用相机类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35166881/

相关文章:

android - 导航到列表末尾时,如何保持 RecyclerView 的最后一项焦点?

python - Tensorflow:Mac OS摄像头已打开但视频在屏幕上不可见

html - html5 相机输入可以在 Windows 10 平板电脑上使用吗?

android - 从相机拍照后图像未在 ImageView 中设置

android - 如何在 Android 的 CameraManager 中注销可用性回调

java - 如何查看Pocketsphinx词典中是否存在该单词?

android放大/缩小在自定义相机应用程序中不起作用

android - Android 上的相机预览处理

android - 组织.json.JSONException : End of input at character

android - 如何解决 SurfaceHolder.setType 已弃用的问题?还有其他方法吗?