android Camera里面的问题:fail to connect to Camera service

clunyes 发布于 2012/05/08 15:47
阅读 46K+
收藏 1

public class CameraActivity extends Activity implements Callback {
 private Button btnClose, btnPhoto, btnPreview;
 private SurfaceView surfaceview;
 private SurfaceHolder surfaceholder;
 private Camera camera;
 private boolean ispreview = false;
 private String picturePath = "/sdcard/zkPic/carema1.jpg";
 private ShutterCallback shuttercallback = new ShutterCallback() {

  @Override
  public void onShutter() {
   // TODO Auto-generated method stub

  }
 };
 private PictureCallback rawcallback = new PictureCallback() {

  @Override
  public void onPictureTaken(byte[] data, Camera camera) {
   // TODO Auto-generated method stub

  }
 };
 private PictureCallback picturecallback = new PictureCallback() {

  @Override
  public void onPictureTaken(byte[] data, Camera camera) {
   // TODO Auto-generated method stub
   Bitmap bm = BitmapFactory.decodeByteArray(data, 0, data.length);
   File picFile = new File(picturePath);
   try {
    BufferedOutputStream bos = new BufferedOutputStream(
      new FileOutputStream(picFile));
    bm.compress(Bitmap.CompressFormat.JPEG, 80, bos);
    resetCamera();
    initCamera();
   } catch (Exception e) {
    System.out.println(e.toString());
   }

  }
 };

 public void setupView() {
  this.btnClose = (Button) findViewById(R.id.btnClose);
  this.btnPhoto = (Button) findViewById(R.id.btnPhoto);
  this.btnPreview = (Button) findViewById(R.id.btnPreview);
  this.surfaceview = (SurfaceView) findViewById(R.id.surfaceView);
  this.surfaceholder = surfaceview.getHolder();
  surfaceholder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
 }

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.camera);
  setupView();
  DisplayMetrics dm = new DisplayMetrics();
  getWindowManager().getDefaultDisplay().getMetrics(dm);
  btnPreview.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    initCamera();
   }
  });
  btnClose.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    resetCamera();
   }
  });
  btnPhoto.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    takePhoto();
   }
  });
 }

 private void initCamera() {
  if (ispreview==false&&camera==null) {
   camera = Camera.open();
  }
  if (camera != null && !ispreview) {
   try {
    /*
     * Camera.Parameters parameters = camera.getParameters();
     * parameters.setPreviewSize(320, 240);
     * parameters.setPictureSize(320, 240);
     * camera.setParameters(parameters);
     */
    camera.setPreviewDisplay(surfaceholder);
    camera.startPreview();

   } catch (Exception e) {
    // TODO: handle exception
   }
   ispreview = true;
  }
 }

 private void takePhoto() {
  if (camera != null && ispreview) {
   camera.takePicture(shuttercallback, rawcallback, picturecallback);
  }
 }

 private void resetCamera() {
  if (camera != null && ispreview) {
   camera.stopPreview();
   camera.release();
   camera = null;
   ispreview = false;
  }
  finish();
 }

 @Override
 public void surfaceChanged(SurfaceHolder holder, int format, int width,
   int height) {
  // TODO Auto-generated method stub

 }

 @Override
 public void surfaceCreated(SurfaceHolder holder) {
  // TODO Auto-generated method stub

 }

 @Override
 public void surfaceDestroyed(SurfaceHolder holder) {
  // TODO Auto-generated method stub

 }

}

 

05-08 15:43:56.493: E/AndroidRuntime(2998): FATAL EXCEPTION: main
05-08 15:43:56.493: E/AndroidRuntime(2998): java.lang.RuntimeException: Fail to connect to camera service
05-08 15:43:56.493: E/AndroidRuntime(2998):  at android.hardware.Camera.native_setup(Native Method)
05-08 15:43:56.493: E/AndroidRuntime(2998):  at android.hardware.Camera.<init>(Camera.java:258)
05-08 15:43:56.493: E/AndroidRuntime(2998):  at android.hardware.Camera.open(Camera.java:220)
05-08 15:43:56.493: E/AndroidRuntime(2998):  at com.zk.pic.CameraActivity.initCamera(CameraActivity.java:111)
05-08 15:43:56.493: E/AndroidRuntime(2998):  at com.zk.pic.CameraActivity.access$2(CameraActivity.java:109)
05-08 15:43:56.493: E/AndroidRuntime(2998):  at com.zk.pic.CameraActivity$4.onClick(CameraActivity.java:88)
05-08 15:43:56.493: E/AndroidRuntime(2998):  at android.view.View.performClick(View.java:2486)
05-08 15:43:56.493: E/AndroidRuntime(2998):  at android.view.View$PerformClick.run(View.java:9130)

 

补充:权限加过了

<user-permission android:name="android.permission.CAMERA"/>
    <user-permission android:name="android.permission.INTERNET"/>

加载中
0
迷途d书童
迷途d书童
真机还是模拟器
0
迷途d书童
迷途d书童
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
//用完要记得释放
public void surfaceDestroyed(SurfaceHolder holder) {
    camera.stopPreview();
    camera.release();
    camera = null;
}

2.3版本中camera.open()函数是有参数的open(int cameraId)

try{
  mCamera = Camera.open();
  }
  catch(RuntimeException e){
   mCamera = Camera.open(Camera.getNumberOfCameras()-1);
   System.out.println("open()方法有问题");
  }

踏雪羽翼
回复 @谢继坤 :
谢继坤
谢继坤
回复 @clunyes : 不明白呀,能不能说清楚一点呀,最下面那个人数说的3个权限都加了呀,还是会有这个错误
clunyes
clunyes
大神,好了,好像就是权限的问题,一个是user一个是uses但是,我在add permission的时候是选user的
clunyes
clunyes
我是真机上测试的,我按你说的在surfacedestroy中释放了camera,open里面也加上你那个参数了,但是还是不行,大神,求指导
0
迷途d书童
迷途d书童
你的相机是不是在被“占用”中
AndyZhai
AndyZhai
你好。如果相机被“占用”中,那该怎么获得这个对象呢?
clunyes
clunyes
为什么parameter不能设置,一设置就报错
0
迷途d书童
迷途d书童

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

加上这3个试试

clunyes
clunyes
恩,对的,加上就行
0
clunyes
clunyes

引用来自“迷途ф書童”的答案

<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
//用完要记得释放
public void surfaceDestroyed(SurfaceHolder holder) {
    camera.stopPreview();
    camera.release();
    camera = null;
}

2.3版本中camera.open()函数是有参数的open(int cameraId)

try{
  mCamera = Camera.open();
  }
  catch(RuntimeException e){
   mCamera = Camera.open(Camera.getNumberOfCameras()-1);
   System.out.println("open()方法有问题");
  }

权限写错了,加上你的就可以
0
TracyZhang
TracyZhang
是你的相机出了问题,重启手机即可,记得修复造成相机挂掉的bug
返回顶部
顶部