android fail to connect to cameraservice错误

    敲了一个android camera 的代码。运行后报:fail to connect to cameraservice的错误,无法解决,在网上搜索了一下,貌似大家都有这个问题,但是还是无法解决,首先,要在AndroidManifest.xml文件中添加权限允许语句:,这个是我忽略了的,本以为添加上去了就可以解决了,但是依旧是这个错误。后来在网上看到一个论坛的回复,他指出,在2.2上运行camera应用没有报错,但是在2.3上运行就会报这个错误,抱着半信半疑的态度,试了一下,果然是这样的,他还指出,是camera.open()函数的问题。2.2版本中camera.open()的open()函数是没有参数的
Camera android.hardware. Camera.open()
public static Camera open()
Since: API Level1

Creates a new Camera object to access the first back-facingcamera on the device. If the device does not have a back-facingcamera, this returns null.

See Also
  • open(int)

而2.3中open函数是有参数的open(int cameraId),

 

Camera android.hardware. Camera.open(intcameraId)
public static Camera open (intcameraId)
Since: API Level9

Creates a new Camera object to access a particular hardwarecamera.

You must call release() when you are done usingthe camera, otherwise it will remain locked and be unavailable toother applications.

Your application should only have one Camera object active at atime for a particular hardware camera.

Callbacks from other methods are delivered to the event loop ofthe thread which called open(). If this thread has no event loop,then callbacks are delivered to the main application event loop. Ifthere is no main application event loop, callbacks are notdelivered.

Caution: On some devices, this method may take a longtime to complete. It is best to call this method from a workerthread (possibly using AsyncTask) to avoidblocking the main application UI thread.

Parameters
cameraIdthe hardware camera to access, between 0 and getNumberOfCameras()-1.
Returns
  • a new Camera object, connected, locked and ready for use.
Throws
RuntimeExceptionif connection to the camera service fails (for example, if thecamera is in use by another process).

为了验证是这个函数的问题,我在camera.open()函数进行出错处理

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

    在2.3上运行,报错是NullPointer空指针错误,也打印出“open()方法有问题”这个语句,说明问题真的处在这个函数上,到底参数是多少呢,API上说cameraId的值在0到getNumberOfCameras()-1之间,试过0,也试过getNumberOfCamera-1,但是依旧报错,看来这个问题暂时还无法解决了。。。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值