Android Camera.open有时会失败

时间:2017-07-19 06:51:47

标签: android exception camera android-camera

我正在使用android vision barcode-reader sample的部分内容来读取QR码。偶尔(比如500个用户中的2个)我收到Firebase崩溃报告,该报告发生在Camera.open。

java.lang.RuntimeException: 
at android.hardware.Camera.<init>(Camera.java:678) 
at android.hardware.Camera.open(Camera.java:520) 
at mobilevision.CameraSource.createCamera(CameraSource.java:0) 
at <OR>.access$2200(CameraSource.java:0) 
at mobilevision.CameraSource.release(CameraSource.java:0) 
at <OR>.start(CameraSource.java:0) 
at <OR>.getIdForRequestedCamera(CameraSource.java:0) 
at <OR>.selectSizePair(CameraSource.java:0) 
at <OR>.generateValidPreviewSizeList(CameraSource.java:0) 
at <OR>.selectPreviewFpsRange(CameraSource.java:0) 
at <OR>.setRotation(CameraSource.java:0) 
at <OR>.createPreviewBuffer(CameraSource.java:0) 
at <OR>.access$102(CameraSource.java:0) 
at <OR>.access$202(CameraSource.java:0) 
at <OR>.access$302(CameraSource.java:0) 
at <OR>.access$502(CameraSource.java:0) 
at <OR>.access$802(CameraSource.java:0) 
at <OR>.access$1800(CameraSource.java:0) 
at mobilevision.CameraSourcePreview.startIfReady(CameraSourcePreview.java:0) 
at mobilevision.CameraSourcePreview.start(CameraSourcePreview.java:0) 
at <OR>.start(CameraSourcePreview.java:0) 
at <OR>.stop(CameraSourcePreview.java:0) 
at <OR>.access$102(CameraSourcePreview.java:0) 
at <OR>.access$200(CameraSourcePreview.java:0) 
at mobilevision.CameraSourcePreview$SurfaceCallback.surfaceCreated(CameraSourcePreview.java:0) 
at android.view.SurfaceView.updateWindow(SurfaceView.java:679) 
at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:206) 
at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:944) 
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2432) 
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1328) 
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6730) 
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:894) 
at android.view.Choreographer.doCallbacks(Choreographer.java:696) 
at android.view.Choreographer.doFrame(Choreographer.java:631) 
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:880) 
at android.os.Handler.handleCallback(Handler.java:815) 
at android.os.Handler.dispatchMessage(Handler.java:104) 
at android.os.Looper.loop(Looper.java:207) 
at android.app.ActivityThread.main(ActivityThread.java:5765) 
at java.lang.reflect.Method.invoke(Method.java:0) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)

抛出异常的代码部分:

int requestedCameraId = getIdForRequestedCamera(mFacing);
if (requestedCameraId == -1) {
  throw new Exception("Could not find requested camera.");
}

Camera camera = Camera.open(requestedCameraId);

我同样要求相机许可,就像在条形码阅读器样本中实现一样。不幸的是,我无法重现这个确切的异常。 但是,在没有摄像头的模拟器中运行应用程序时,我能够重现类似的异常(两个摄像头都设置为无)。但由于这种异常发生在真实设备中,我想知道可能是什么原因。 崩溃报告中的电话是例如Galaxy J1(2016)(j1x3g),A5.1,Plume P6 Pro LTE(PGN528),A6.0,3600i(CP3600I),A6.0。

任何人都可以给我一个关于如何重现此异常甚至更好的提示,是否有人知道即使获得许可也无法启动相机的原因?

非常感谢!

1 个答案:

答案 0 :(得分:0)

很高兴回答你的问题。

有相应的checkPermission()方法,但这些方法仅确定是否已在清单文件中注册了权限。用户的操作无法访问。

try{} catch){}

那么我们如何在6.0以下的系统中检测权限? 本方法是使用public boolean isCameraCanUse() { boolean isCanUse = true; Camera mCamera = null; try { mCamera = Camera.open(); Camera.Parameters mParameters = mCamera.getParameters(); //针对魅族手机 mCamera.setParameters(mParameters); } catch (Exception e) { isCanUse = false; } if (mCamera != null) { try { mCamera.release(); } catch (Exception e) { e.printStackTrace(); return isCanUse; } } return isCanUse; } 方式:

String str = "5F2A";
int number = someOperation(str);

我希望这会有所帮助。