我在StackOverflow上搜索了很多关于此错误但未能找到解决方案的信息。
这是例外:
java.lang.RuntimeException: Fail to connect to camera service
at android.hardware.Camera.native_setup(Native Method)
at android.hardware.Camera.<init>(Camera.java:351)
at android.hardware.Camera.open(Camera.java:325)
at com.app.app.Scatta.surfaceCreated(Scatta.java:232)
at android.view.SurfaceView.updateWindow(SurfaceView.java:572)
at android.view.SurfaceView.access$000(SurfaceView.java:86)
at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:175)
at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:847)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1867)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:996)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5600)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:544)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
在Camera.open
void surfaceCreated
时应用崩溃
这是我的代码:
public class Scatta extends Activity implements SurfaceHolder.Callback {
android.hardware.Camera camera;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.scatta_layout);
surfaceView = (SurfaceView) findViewById(R.id.camerapreview);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
controlInflater = LayoutInflater.from(getBaseContext());
viewControl = controlInflater.inflate(R.layout.camera_layout, null);
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
this.addContentView(viewControl, layoutParams);
RelativeLayout relativeLayout = (RelativeLayout) viewControl.findViewById(R.id.relative_camera);
relativeLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
camera.autoFocus(autoFocusCallback);
}
});
...
@Override
public void surfaceCreated(SurfaceHolder holder) {
if (camera == null){
camera = android.hardware.Camera.open(); ////////this is the row 232
try{
camera.setPreviewDisplay(holder);
}catch (IOException e) {
e.printStackTrace();
camera.release();
camera = null;
}
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
if (previewing) {
camera.stopPreview();
}
else {
android.hardware.Camera.Parameters parameters = camera.getParameters();
Display display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
if (display.getRotation() == Surface.ROTATION_90){
parameters.setPreviewSize(width, height);
}
if (display.getRotation() == Surface.ROTATION_0) {
parameters.setPreviewSize(height, width);
camera.setDisplayOrientation(90);
}
if (display.getRotation() == Surface.ROTATION_180) {
parameters.setPreviewSize(height, width);
}
if (display.getRotation() == Surface.ROTATION_270) {
parameters.setPreviewSize(width, height);
camera.setDisplayOrientation(180);
}
parameters.setFocusMode(android.hardware.Camera.Parameters.FOCUS_MODE_AUTO);
List<android.hardware.Camera.Size> sizes = parameters.getSupportedPictureSizes();
size = sizes.get(0);
parameters.setPictureSize(size.width, size.height);
camera.setParameters(parameters);
Thread preview_thread = new Thread(new Runnable() {
@Override
public void run() {
camera.startPreview();
}
}, "preview_thread");
preview_thread.start();
previewing = true;
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
if (camera != null){
camera.stopPreview();
camera.release();
camera = null;
}
}
这是AndroidManifest.xml
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.FLASHLIGHT"/>
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<application
...
奇怪的是,在三星Galaxy Core2中我看不到这个问题,但在Honor 6中还有模拟器(Genymotion)
使用相同版本的Android,设备如何工作但不能在另一个设备上工作?
答案 0 :(得分:0)
写入你的gradle.build targetSdkVersion 22.这是一个临时的解决方案,但是工作。