错误:无法连接到相机服务@Android marshmallow

时间:2016-05-26 10:26:51

标签: android camera

我正在尝试构建一个相机应用程序(我很陌生) 当我在真实设备上运行应用程序时,它给出了我的错误:

    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." />

3 个答案:

答案 0 :(得分:7)

您需要以编程方式授予android.hardware.Camera权限。

  

在Android 6上完成工作的明确许可

     

使用marshmallow(Android的最新版本)。我们有一些   使用敏感权限的限制,例如:   存储,联系人访问等。在版本中提供这些权限   清单,我们需要在运行时中向用户请求。

有关详细信息,请参阅: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);
        }
    }
}

答案 1 :(得分:1)

在marshmellow之后,OS permisson应该在运行时使用。摄像机权限也必须在运行时获取请检查以下链接以了解如何在运行时询问权限。 https://developer.android.com/training/permissions/requesting.html

快速解决方案: 这是一个修复,但您可以使用当前的marshmellow版本 尝试在build.gradle文件中添加它

targetSdkVersion 22

答案 2 :(得分:1)

在Marshmallow上,您必须在运行时向用户询问授权许可

int permissionCheck = ContextCompat.checkSelfPermission(thisActivity,
        Manifest.permission.WRITE_CALENDAR);

if(permissionCheck == PackageManager.PERMISSION_GRANTED){
//user granted you
}else{
//the user denied your permission
}