简体   繁体   English

相机错误“无法连接到相机”或某些手机中出现错误“相机正在使用其他应用”

[英]Camera Error “Can't Connect to the Camera” or in some phones the error appear “Camera is using by another app”

I have implemented to record audio in background using Android MediaRecorder if the audio recording is in progress and user open's the native camera to record video, it gives 我已经实现了使用Android MediaRecorder在后台录制音频,如果录音正在进行中并且用户打开了原生相机来录制视频,它给出了

Camera Error "Can't Connect to the Camera" 相机错误“无法连接到相机”

or on some phones, the error appears as 或者在某些手机上,错误显示为

Your camera is in use by another application 您的相机正在被其他应用程序使用

在此输入图像描述

If I stop mediarecorder then the native camera video recording works fine, I searched for events to know when Camera is going to start video so then in my app I stop the mediarecorder, I found the BroadcastReceiver with filters 如果我停止mediarecorder然后原生相机视频录制工作正常,我搜索事件,以了解相机何时开始视频,然后在我的应用程序中我停止mediarecorder,我发现BroadcastReceiver带过滤器

        <receiver android:name=".receiver.CameraReceiver">
            <intent-filter android:priority="10000">
            <action android:name="android.Medintent.action.CAMERA_BUTTON" />
            <action android:name="android.hardware.action.NEW_PICTURE" />
            <action android:name="android.hardware.action.NEW_VIDEO" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="image/*" />
            <data android:mimeType="video/*" />
        </intent-filter>
    </receiver>

NEW_VIDEO and NEW_PICTURE events fired when picture or video is captured and saved in the directory. 在捕获图片或视频并将其保存在目录中时触发NEW_VIDEONEW_PICTURE事件。 Anyone knows how can this issue be solved? 任何人都知道如何解决这个问题? I want to know in my app the event when Native/Camera Apps going to record video. 我希望在我的应用程序中知道Native / Camera Apps要录制视频时的事件。 Thanks in advance 提前致谢

Even i had the same issue. 即使我有同样的问题。 Once camera resource is being used by an application,untill it is released, you can use them in some other app or even a service. 一旦应用程序正在使用相机资源,直到它被释放,您可以在其他应用程序甚至服务中使用它们。 If any service is using the camera resource, untill it releases the same we cannot use camera hardware. 如果任何服务正在使用相机资源,直到它发布相同的我们不能使用相机硬件。 You can doublecheck if camera hardware is being used using this code :- 如果使用此代码正在使用相机硬件,您可以重复检查: -

 private boolean isCameraInUse() {
    Log.v(TAG, "isCameraInUse()");
    boolean isCameraInUse = false;
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)//Use Camera Api for Version Code < 23 and mCamera manager above it.
    {
        String cameraId = null;
        CameraManager camManager = (CameraManager) mContext.getSystemService(Context.CAMERA_SERVICE);
        // Usually front mCamera is at 0 position.
        try {
            cameraId = camManager.getCameraIdList()[0];
        } catch (CameraAccessException e) {
            Log.e(TAG, Log.getStackTraceString(e));
            isCameraInUse = true;
        }
        try {
            camManager.setTorchMode(cameraId, true);
            camManager.setTorchMode(cameraId, false);
        } catch (CameraAccessException e) {
            Log.e(TAG, Log.getStackTraceString(e));
            isCameraInUse = true;
        }
    } else {
        Camera c = null;
        try {
            c = Camera.open();
        } catch (RuntimeException e) {
            Log.e(TAG, Log.getStackTraceString(e));
            turnFlashOff(mContext);
            isCameraInUse = true;
        } finally {
            if (c != null) c.release();
        }
    }
    return isCameraInUse;
}

CameraManager.AvailabilityCallback provides onCameraAvailable(String cameraId) method to detect whether a camera is available or not. CameraManager.AvailabilityCallback提供onCameraAvailable(String cameraId)方法来检测相机是否可用。 https://developer.android.com/reference/android/hardware/camera2/CameraManager.AvailabilityCallback.html https://developer.android.com/reference/android/hardware/camera2/CameraManager.AvailabilityCallback.html

Getting all camera IDs is the same as the above as shown by @GAGAN. 获取所有摄像机ID与@GAGAN所示的相同。

CameraManager camManager = (CameraManager) mContext.getSystemService(Context.CAMERA_SERVICE);
        // Usually front mCamera is at position 0.
        try {
            cameraId = camManager.getCameraIdList()[0];
        }  catch (CameraAccessException e) {
            Log.e(TAG, Log.getStackTraceString(e));
            Log.cat("Error: ", e.getReason() + "");
            if (e.CAMERA_DISABLED) { /* ENABLE CAMERA */ }
            else if ( /* ... you can go to the link below to do various logic */ ) {
                //...
            }
            else { /* do nothing */ }
        }

https://developer.android.com/reference/android/hardware/camera2/CameraAccessException.html#CAMERA_IN_USE https://developer.android.com/reference/android/hardware/camera2/CameraAccessException.html#CAMERA_IN_USE

Note: Until other applications use your camera hardware, they are using purposefully used by them. 注意: 在其他应用程序使用您的相机硬件之前,它们是由他们有目的地使用。 So until these apps don't free your hardware, you can't use that, it's clear. 因此,在这些应用程序不能释放您的硬件之前,您无法使用它,这很明显。 You can't know whether these apps actually need the camera. 你无法知道这些应用程序是否真的需要相机。 We believe process reserves hardware when it needs. 我们相信流程可以在需要时保留硬件。

But, we can setup the listener on when camera becomes available (free) so that you can use it immediately. 但是,我们可以在相机可用(免费)时设置监听器,以便您可以立即使用它。

CameraManager.AvailabilityCallback availabilityCallback = new CameraManager.AvailabilityCallback() {
    @Override 
    public void onCameraAvailable(String cameraId) {
        // your further stuffs. You must put all of your camera related logic here. 
    }

    public void onCameraUnavailable(String cameraId) {
        //you can logcat camera not available 
    }
};

The use of CameraManager.AvailabilityCallback abstract class is when you instantiate object, you do anonymous instantiation implementing callbacks like onCameraAvailable() , which is actually a listener that you are getting from the Camera Observer . 使用CameraManager.AvailabilityCallback抽象类是在实例化对象时,您执行匿名实例化实现回调,如onCameraAvailable() ,它实际上是您从Camera Observer获取的listener

Answer: If you would have put your camera processing commands inside onCameraAvailable() callback, I guarantee, you wouldn't have got the error that you showed. 答:如果您将相机处理命令放在onCameraAvailable()回调中,我保证,您不会得到您显示的错误。

If the camera is used by other apps in the background, it means, those other apps are kind of buggy, because the camera needs foreground preview; 如果相机在后台被其他应用程序使用,则意味着,那些其他应用程序有点儿错误,因为相机需要前景预览; they are not releasing the camera even after they are done with it. 即使完成相机,它们也不会释放相机。 Nobody uses camera in background. 没有人在背景中使用相机。 Even in this case also, you are not supposed to kill camera process forcefully, because memory leaks can happen. 即使在这种情况下,也不应该强行杀死相机进程,因为内存泄漏可能会发生。 Because you don't know how other processes are using the camera. 因为您不知道其他进程如何使用相机。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM