java.lang.RuntimeException:无法连接到摄像头服务。应用程序每次都崩溃了

时间:2014-07-24 15:01:34

标签: java android android-camera

我很抱歉再次提出这个问题,但我已经解决了这个问题。

我正在尝试创建一个简单的相机应用程序。当我运行我的代码时,我从日志中收到"Caused by: java.lang.RuntimeException: Fail to connect to camera service"错误。我添加了" uses-premission"到我的manifest,但代码仍然崩溃。我甚至无法打开相机一次(因此没有相机没有发布问题)。

提前致谢,

我的代码

package com.example.bn.cameratest;



import android.app.Activity;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import java.util.List;

public class MyActivity extends Activity implements SurfaceHolder.Callback {
Camera mCamera;
SurfaceView mPreview;

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);

    mPreview = (SurfaceView)findViewById(R.id.preview);
    mPreview.getHolder().addCallback(this);
    //Android 3.0 support
   // mPreview.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        mCamera = Camera.open(0);


}



@Override
public void onPause(){
    super.onPause();
    mCamera.stopPreview();
}

@Override
public void onDestroy(){
    super.onDestroy();
    mCamera.release();
}

//Methods required by surface callback

@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
    try{

        mCamera.setPreviewDisplay(mPreview.getHolder());

    }catch (Exception e){
        e.printStackTrace();
    }
}

@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i2, int i3) {
    Camera.Parameters params = mCamera.getParameters();

    List<Camera.Size> sizes = params.getSupportedPreviewSizes();
    Camera.Size selected = sizes.get(0);
    params.setPreviewSize(selected.width,selected.height);
    mCamera.setParameters(params);

    mCamera.startPreview();
}

@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {

}


}

我的清单

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />


<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <activity
        android:name=".MyActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

0 个答案:

没有答案