无法连接相机服务

时间:2016-04-07 09:01:21

标签: android android-camera front-camera

我已经使用了所有必需的权限,这些权限是:

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

我使用的代码是:

private Camera openFrontFacingCameraGingerbread()
{ int cameraCount = 0;
    Camera cam = null;
    Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
    cameraCount = Camera.getNumberOfCameras();
    for (int camIdx = 0; camIdx < cameraCount; camIdx++)
    { Camera.getCameraInfo(camIdx, cameraInfo);
        if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT)
        { try { cam = Camera.open(camIdx);
        } catch (RuntimeException e)
        { Log.e(TAG, "Camera failed to open: " + e.getLocalizedMessage());
        }
        }
    } return cam;
}

但错误即将来临。 Logcat错误:

Cannot connect camera service

以下提供的解决方案的Logcat,

FATAL EXCEPTION: main
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime: Process: pop.lock.com.frocam, PID: 4090
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime: java.lang.IllegalStateException: Could not execute method of the activity
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime:     at android.view.View$1.onClick(View.java:3848)
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime:     at android.view.View.performClick(View.java:4463)
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime:     at android.view.View$PerformClick.run(View.java:18770)
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:808)
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:103)
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:193)
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5333)
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515)
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime:  Caused by: java.lang.reflect.InvocationTargetException
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515)
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime:     at android.view.View$1.onClick(View.java:3843)
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime:     at android.view.View.performClick(View.java:4463) 
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime:     at android.view.View$PerformClick.run(View.java:18770) 
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:808) 
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:103) 
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:193) 
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5333) 
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method) 
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515) 
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828) 
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644) 
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method) 
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime:  Caused by: java.lang.NullPointerException
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime:     at pop.lock.com.frocam.FrontCamActivity.onClick(FrontCamActivity.java:41)
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method) 
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515) 
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime:     at android.view.View$1.onClick(View.java:3843) 
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime:     at android.view.View.performClick(View.java:4463) 
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime:     at android.view.View$PerformClick.run(View.java:18770) 
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:808) 
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:103) 
04-07 16:25:40.935 4090-4090/? E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:193) 

我正在使用compilesdkversion 23,使用我的4.4.2 kitkat HTC 816g手机来运行该应用程序。任何与相机服务相关的代码都没有执行。

1 个答案:

答案 0 :(得分:0)

尝试以下更新代码。

<强> activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <FrameLayout
        android:id="@+id/camPreview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" >
    </FrameLayout>
</RelativeLayout>

<强> MainActivity.java

 public class MainActivity extends Activity {
        private Camera mCam;
        private MirrorView mCamPreview;
        private int mCameraId = 0;
        private FrameLayout mPreviewLayout;

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

           mCameraId = findFirstFrontFacingCamera();

            mPreviewLayout = (FrameLayout) findViewById(R.id.camPreview);
            mPreviewLayout.removeAllViews();

            startCameraInLayout(mPreviewLayout, mCameraId);

        }
        private int findFirstFrontFacingCamera() {
            int foundId = -1;
            int numCams = Camera.getNumberOfCameras();
            for (int camId = 0; camId < numCams; camId++) {
                CameraInfo info = new CameraInfo();
                Camera.getCameraInfo(camId, info);
                if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
                    foundId = camId;
                    break;
                }
            }
            return foundId;
        }

        private void startCameraInLayout(FrameLayout layout, int cameraId) {
            mCam = Camera.open(cameraId);
            if (mCam != null) {
                mCamPreview = new MirrorView(this, mCam);
                layout.addView(mCamPreview);
            }
        }


        public class MirrorView extends SurfaceView implements
        SurfaceHolder.Callback {
    private SurfaceHolder mHolder;
    private Camera mCamera;

    public MirrorView(Context context, Camera camera) {
        super(context);
        mCamera = camera;
        mHolder = getHolder();
        mHolder.addCallback(this);
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    public void surfaceCreated(SurfaceHolder holder) {
        try {
            mCamera.setPreviewDisplay(holder);
            mCamera.startPreview();
        } catch (Exception error) {

        }
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int w,
            int h) {
        if (mHolder.getSurface() == null) {
            return;
        }

        // can't make changes while mPreviewLayout is active
        try {
            mCamera.stopPreview();
        } catch (Exception e) {

        }

        try {

            // start up the mPreviewLayout
            mCamera.setPreviewDisplay(mHolder);
            mCamera.startPreview();

        } catch (Exception error) {

        }
    }
  }
}

<强>的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.camera.frtex"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="15" />
    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".MainActivity"
            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>
</manifest>