try { // attempt to get a Front Camera instance c = Camera.open(Camera.CameraInfo.CAMERA_FACING_FRONT); } catch (Exception e) { // Todo auto-generated catch block System.out .println("fail to connect to Front Camera"); } if (c == null) { try { // attempt to get a Back Camera instance c = Camera.open(1); } catch (Exception e) { // Todo: handle exception System.out .println("fail to connect to Camera with ID = 1"); } } if (c == null) { try { // attempt to get a Back Camera instance c = Camera.open(0); } catch (Exception e) { // Todo: handle exception System.out .println("fail to connect to Camera with ID = 0"); } } if (c == null) { try { // attempt to get a Back Camera instance c = Camera.open(); } catch (Exception e) { // Todo: handle exception System.out .println("fail to connect to Back Camera"); return c; }
其中c是Camera的对象.
它在除Nexus 7平板电脑之外的其他手机中运行良好.在Nexus 7中,代码在所有情况下抛出异常,除了最后一个,即c = Camera.open();,但对象c仍为null.
这是堆栈跟踪
11-22 12:36:57.559 W/System.err(7621): java.lang.NullPointerException11-22 12:36:57.559 W/System.err(7621): at com.MyPackage.OpenCamera.getFrontCameraInstance(OpenCamera.java:238)11-22 12:36:57.559 W/System.err(7621): at com.MyPackage.OpenCamera.onCreate(OpenCamera.java:123)11-22 12:36:57.559 W/System.err(7621): at androID.app.Activity.performCreate(Activity.java:5104)11-22 12:36:57.559 W/System.err(7621): at androID.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)11-22 12:36:57.559 W/System.err(7621): at androID.app.ActivityThread.performlaunchActivity(ActivityThread.java:2144)11-22 12:36:57.559 W/System.err(7621): at androID.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)11-22 12:36:57.559 W/System.err(7621): at androID.app.ActivityThread.access0(ActivityThread.java:141)11-22 12:36:57.559 W/System.err(7621): at androID.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)11-22 12:36:57.559 W/System.err(7621): at androID.os.Handler.dispatchMessage(Handler.java:99)11-22 12:36:57.559 W/System.err(7621): at androID.os.Looper.loop(Looper.java:137)11-22 12:36:57.559 W/System.err(7621): at androID.app.ActivityThread.main(ActivityThread.java:5039)11-22 12:36:57.559 W/System.err(7621): at java.lang.reflect.Method.invokeNative(Native Method)11-22 12:36:57.559 W/System.err(7621): at java.lang.reflect.Method.invoke(Method.java:511)11-22 12:36:57.559 W/System.err(7621): at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)11-22 12:36:57.559 W/System.err(7621): at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:560)11-22 12:36:57.559 W/System.err(7621): at dalvik.system.NativeStart.main(Native Method)11-22 12:36:57.559 I/System.out(7621): Error in setting Parameter11-22 12:36:57.609 I/ActivityManager(480): displayed 11-22 12:36:57.679 W/System.err(7621): at androID.location.Geocoder.getFromLocation(Geocoder.java:136)11-22 12:36:57.679 W/System.err(7621): at com.MyPackage.OpenCamera.$MyTimmer.run(OpenCamera.java:336)11-22 12:36:57.679 W/System.err(7621): java.lang.NullPointerException11-22 12:36:57.679 W/System.err(7621): at com.MyPackage.OpenCamera.$MyTimmer.run(OpenCamera.java:344)
所以我不能用它来捕捉图像.
有什么办法吗
谢谢…
解决方法 在Nexus 7上使用以下代码时:int cameraID = CameraInfo.CAMERA_FACING_FRONT;
但是,如您所见,cameraID为1,使用1作为参数会导致错误.
我发现使用0代替1可以工作,但每次使用后必须释放相机,所以我继续调用Camera.open(cameraID).release();
我意识到这很难看,但它似乎解决了Nexus 7的问题.
int cameraID = -1; boolean errorFound = false; boolean hasFeatCamera = getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA); if (hasFeatCamera) { try{ cameraID = CameraInfo.CAMERA_FACING_BACK; Camera.open(cameraID).release(); }catch(Exception e){ Camera.open(0).release(); } } else if(CameraInfo.CAMERA_FACING_FRONT>-1){ try{ cameraID = CameraInfo.CAMERA_FACING_FRONT; Camera.open(cameraID).release(); }catch(Exception e){ errorFound = true; } if(errorFound){ try{ Camera.open(0).release(); // Put in for Nexus 7 as CameraInfo.CAMERA_FACING_FRONT= 1 but it only loads if the ID is actually 0 cameraID = 0; }catch(Exception e){ cameraID = -1; } } } if(cameraID<0){ Toast.makeText(this,"No camera found.",Toast.LENGTH_LONG).show(); }总结
以上是内存溢出为你收集整理的android – 在Nexus 7平板电脑中打开相机全部内容,希望文章能够帮你解决android – 在Nexus 7平板电脑中打开相机所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)