无法连接相机服务
Cannot connect camera service
我已经使用了所有需要的权限,它们是:
<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.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.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.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 mobile 运行 应用程序。任何与相机服务相关的代码都没有执行。
试试下面的更新代码。
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>
我已经使用了所有需要的权限,它们是:
<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.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.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.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 mobile 运行 应用程序。任何与相机服务相关的代码都没有执行。
试试下面的更新代码。
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>