错误:无法连接到相机服务@Android marshmallow
Posted
技术标签:
【中文标题】错误:无法连接到相机服务@Android marshmallow【英文标题】:ERROR: failed to connect to camera service @ Android marshmallow 【发布时间】:2016-09-24 07:10:09 【问题描述】:我正在尝试构建一个相机应用程序(我对此很陌生) 当我在真实设备上运行应用程序时,它给了我这个错误:
05-26 13:16:37.504 14076-14076/involved.pose9 E/androidRuntime: FATAL EXCEPTION: main
Process: involved.pose9, PID: 14076
java.lang.RuntimeException: Fail to connect to camera service
at android.hardware.Camera.<init>(Camera.java:511)
at android.hardware.Camera.open(Camera.java:368)
at involved.pose9.CameraActivity.surfaceCreated(CameraActivity.java:63)
at android.view.SurfaceView.updateWindow(SurfaceView.java:582)
at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:177)
at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:944)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2067)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1119)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6060)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:746)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
这是我的课
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import android.view.WindowManager;
import java.io.IOException;
public class CameraActivity extends AppCompatActivity implements SurfaceHolder.Callback
SurfaceHolder mSurfaceHolder;
SurfaceView mSurfaceView;
public Camera mCamera;
boolean mPreviewRunning;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
getWindow().setFormat(PixelFormat.TRANSLUCENT);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_camera);
mSurfaceView = (SurfaceView) findViewById(R.id.surface_camera);
mSurfaceHolder = mSurfaceView.getHolder();
mSurfaceHolder.addCallback(this);
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3)
// TODO Auto-generated method stub
if (mPreviewRunning)
mCamera.stopPreview();
Camera.Parameters p = mCamera.getParameters();
//p.setPreviewSize(w, h);
mCamera.setParameters(p);
try
mCamera.setPreviewDisplay(arg0);
catch (IOException e)
e.printStackTrace();
mCamera.startPreview();
mPreviewRunning = true;
@Override
public void surfaceCreated(SurfaceHolder holder)
// TODO Auto-generated method stub
mCamera = Camera.open();
@Override
public void surfaceDestroyed(SurfaceHolder holder)
// TODO Auto-generated method stub
mCamera.stopPreview();
mPreviewRunning = false;
mCamera.release();
Camera.PictureCallback mPictureCallback = new Camera.PictureCallback()
public void onPictureTaken(byte[] imageData, Camera c)
;
这里有什么问题?提前谢谢..
我还在 manifest.xml 中声明了权限
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.Camera" />
<uses-feature android:name="android.hardware." />
【问题讨论】:
对于 marshamllow 你需要 RunTimePermission 请参考这个developer.android.com/training/permissions/requesting.html @AbhishekPatel 如果是这样的话,例外情况会不会有所不同?虽然你是对的,但在 M 中你需要检查权限。鉴于日志,我不确定情况是否如此。 @Allie 如果它的权限问题你会得到这个错误。您将无法访问相机并且在清单中请求权限将不起作用:) Android Marshmallow : java.lang.RuntimeException: Fail to connect to camera service的可能重复 那我该怎么办?我已经阅读了帕特尔先生发给我的链接,但我不明白该怎么做.. 感谢您的快速回复,抱歉让我成为菜鸟... 【参考方案1】:您需要以编程方式授予android.hardware.Camera
权限。
清单权限在 Android 6 上不起作用
使用棉花糖(最新版本的 Android)。我们有一些 使用 Sensitive 权限的限制,例如: 存储、联系人访问等。在版本中授予这些权限 manifest,我们需要在 Runtime 向用户请求它们。
更多详情请参考:Android M permissions
在您的活动中添加此代码:
@Override
protected void onStart()
super.onStart();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
int hasCameraPermission = checkSelfPermission(Manifest.permission.CAMERA);
List<String> permissions = new ArrayList<String>();
if (hasCameraPermission != PackageManager.PERMISSION_GRANTED)
permissions.add(Manifest.permission.CAMERA);
if (!permissions.isEmpty())
requestPermissions(permissions.toArray(new String[permissions.size()]), 111);
在onActivityResult
之后添加这个:
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults)
switch (requestCode)
case 111:
for (int i = 0; i < permissions.length; i++)
if (grantResults[i] == PackageManager.PERMISSION_GRANTED)
System.out.println("Permissions --> " + "Permission Granted: " + permissions[i]);
else if (grantResults[i] == PackageManager.PERMISSION_DENIED)
System.out.println("Permissions --> " + "Permission Denied: " + permissions[i]);
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
【讨论】:
【参考方案2】:从 marshmellow 开始,操作系统权限应该在运行时获得。相机权限也必须在运行时获取请查看以下链接以了解如何在运行时请求权限。 https://developer.android.com/training/permissions/requesting.html
快速解决方案: 这是一个修复,但您可以用于当前的 marshmellow 版本 尝试将其添加到您的 build.gradle 文件中
targetSdkVersion 22
【讨论】:
这行得通,但它不会在 Marshmallow 设备上运行.. 不过谢谢.. :) 不会在应用程序没有在 marshmellow 中启动的意义上运行? @AllieFly 它将在棉花糖中运行。但默认情况下,所有权限都将被授予。最好在运行时处理权限,而不是将 targetSdk 版本降低到 23【参考方案3】:在 Marshmallow 上,您必须在运行时请求用户授予权限,例如
int permissionCheck = ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.WRITE_CALENDAR);
if(permissionCheck == PackageManager.PERMISSION_GRANTED)
//user granted you
else
//the user denied your permission
【讨论】:
但这是用于日历的。相机有什么权限,我应该在哪里写这个?我还应该删除清单权限吗? 不,您必须保留清单权限,是的,只需编辑代码并将 CALENDAR 替换为 CAMERA 权限以上是关于错误:无法连接到相机服务@Android marshmallow的主要内容,如果未能解决你的问题,请参考以下文章
Android 6.0 RuntimeException:无法连接到相机服务
Tensorflow android demo出错了。无法连接到相机服务
android:无法在 targetSdkVersion 更改时连接到相机服务