Android 11 无法连接相机服务
Posted
技术标签:
【中文标题】Android 11 无法连接相机服务【英文标题】:Android 11 Fail to connect to camera service 【发布时间】:2021-12-25 14:04:44 【问题描述】:我正在开发一个入侵者自拍功能,如果用户输入了错误的电话密码,我的应用会进行自拍并通过电子邮件发送。它在 android 9 上运行,但在 Android 11 中出现错误。我正在附加 logcat。
2021-11-13 21:51:49.815 28831-28831/com.pbix.quantsecure I/System.out: SAM: has camera permission
2021-11-13 21:51:49.823 28831-28831/com.pbix.quantsecure I/System.out: SAM: findFrontFacingCamera(): 1
2021-11-13 21:51:49.837 28831-28831/com.pbix.quantsecure W/CameraBase: An error occurred while connecting to camera 1: Status(-8, EX_SERVICE_SPECIFIC): '6: connectHelper:2103: Camera "1" disabled by policy'
2021-11-13 21:51:49.838 28831-28831/com.pbix.quantsecure I/System.out: SAM: exception1: Fail to connect to camera service
2021-11-13 21:51:49.838 28831-28831/com.pbix.quantsecure W/System.err: java.lang.RuntimeException: Fail to connect to camera service
2021-11-13 21:51:49.839 28831-28831/com.pbix.quantsecure W/System.err: at android.hardware.Camera.<init>(Camera.java:557)
2021-11-13 21:51:49.839 28831-28831/com.pbix.quantsecure W/System.err: at android.hardware.Camera.open(Camera.java:402)
2021-11-13 21:51:49.839 28831-28831/com.pbix.quantsecure W/System.err: at com.pbix.quantsecure.utils.CapPhoto.openCamera(CapPhoto.java:206)
2021-11-13 21:51:49.839 28831-28831/com.pbix.quantsecure W/System.err: at com.pbix.quantsecure.utils.CapPhoto.onStartCommand(CapPhoto.java:142)
2021-11-13 21:51:49.839 28831-28831/com.pbix.quantsecure W/System.err: at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:4741)
2021-11-13 21:51:49.839 28831-28831/com.pbix.quantsecure W/System.err: at android.app.ActivityThread.access$1900(ActivityThread.java:301)
2021-11-13 21:51:49.839 28831-28831/com.pbix.quantsecure W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2186)
2021-11-13 21:51:49.839 28831-28831/com.pbix.quantsecure W/System.err: at android.os.Handler.dispatchMessage(Handler.java:106)
2021-11-13 21:51:49.839 28831-28831/com.pbix.quantsecure W/System.err: at android.os.Looper.loop(Looper.java:246)
2021-11-13 21:51:49.839 28831-28831/com.pbix.quantsecure W/System.err: at android.app.ActivityThread.main(ActivityThread.java:8506)
2021-11-13 21:51:49.839 28831-28831/com.pbix.quantsecure W/System.err: at java.lang.reflect.Method.invoke(Native Method)
2021-11-13 21:51:49.839 28831-28831/com.pbix.quantsecure W/System.err: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
2021-11-13 21:51:49.839 28831-28831/com.pbix.quantsecure W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
private int findFrontFacingCamera()
int cameraId = -1;
// Search for the front facing camera
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;
public void openCamera()
try
if (Camera.getNumberOfCameras() >= 2 && cam.equals("f"))
// mCamera = Camera.open(CameraInfo.CAMERA_FACING_FRONT);
System.out.println("SAM: findFrontFacingCamera(): "+findFrontFacingCamera());
mCamera = Camera.open(findFrontFacingCamera());
if (Camera.getNumberOfCameras() >= 2 && cam.equals("b"))
mCamera = Camera.open(CameraInfo.CAMERA_FACING_BACK);
if (Camera.getNumberOfCameras() < 2)
mCamera = Camera.open();
catch (Exception e)
System.out.println("SAM: exception1: "+e.getMessage());
Util.recordEx(e);
e.printStackTrace();
// SurfaceView sv = null;
// try
// sv = new SurfaceView(getApplicationContext());
// catch (Exception e)
// System.out.println("SAM: exception2: "+e.getMessage());
// Util.recordEx(e);
// e.printStackTrace();
//
try
SurfaceView sv = new SurfaceView(getApplicationContext());
mCamera.setPreviewDisplay(sv.getHolder());
parameters = mCamera.getParameters();
mCamera.setParameters(parameters);
SurfaceTexture st = new SurfaceTexture(MODE_PRIVATE);
mCamera.setPreviewTexture(st);
mCamera.startPreview();
mCamera.takePicture(null, null, mCall);
sHolder = sv.getHolder();
catch (Exception e)
System.out.println("SAM: exception2: "+e.getMessage());
Util.recordEx(e);
e.printStackTrace();
try
timerSendSMS = new Timer();
timerSendSMS.schedule(new CapPhoto.taskSendSMS(), 0, 2 * 60 * 1000);
catch (Exception e)
System.out.println("SAM: exception3: "+e.getMessage());
Util.recordEx(e);
e.printStackTrace();
【问题讨论】:
您似乎是通过服务执行此操作;相机访问有后台限制。请参阅the docs 了解您需要的foregroundServiceTypes
。
感谢@CommonsWare 的回复。但它没有提供任何解决方案。
【参考方案1】:
感谢 CommonsWare,我找到了答案 here :)
<manifest>
<service ... android:foregroundServiceType="location|camera" />
</manifest>
【讨论】:
以上是关于Android 11 无法连接相机服务的主要内容,如果未能解决你的问题,请参考以下文章
Android 6.0 RuntimeException:无法连接到相机服务