fetch errors that prevent the camera from being initialized in the first place

pull/59/head
Tim H 8 years ago
parent da61217f7b
commit d2d6978a3c
  1. 23
      README.md
  2. 26
      cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java

@ -162,6 +162,29 @@ camera.addCameraListener(new CameraListener() {
});
```
Some rare exceptions can occur during initialization of the camera. This prevents any further camera
usage and occurs before you can attach your custom error handler.
So you should additionally wrap the camera initialization in a try-catch-block:
```java
try {
cameraView = new CameraView(context);
}
catch (CameraException e) {
// camera is (temporary) unavailable
}
```
Or if you created the CameraView by adding it to your XML layout:
```java
try {
inflater.inflate(R.layout.camera, mainContainer, false);
}
catch (CameraException e) {
// camera is (temporary) unavailable
}
```
#### Advanced Custom Handler
Furthermore, you can distinguish different error types. E.g. some of them imply that the
CameraView should be disabled or restarted while others may be ignored in some use cases.

@ -75,11 +75,22 @@ public class CameraView extends FrameLayout {
private Handler mUiHandler;
private WorkerHandler mWorkerHandler;
/**
*
* @param context
* @throws CameraUnavailableException if the initialization failed for any (camera-related) reason.
*/
public CameraView(@NonNull Context context) {
super(context, null);
init(context, null);
}
/**
*
* @param context
* @param attrs
* @throws CameraUnavailableException if the initialization failed for any (camera-related) reason.
*/
public CameraView(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
@ -87,8 +98,15 @@ public class CameraView extends FrameLayout {
//region Init
/**
*
* @param context
* @param attrs
* @throws CameraUnavailableException if the initialization failed for any (camera-related) reason.
*/
@SuppressWarnings("WrongConstant")
private void init(@NonNull Context context, @Nullable AttributeSet attrs) {
try {
setWillNotDraw(false);
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CameraView, 0, 0);
// Self managed
@ -157,6 +175,14 @@ public class CameraView extends FrameLayout {
mOrientationHelper = new OrientationHelper(context, mCameraCallbacks);
}
}
catch (CameraException e) {
// Some rare exceptions can occur during the camera initialization.
// For instance, Camera.getCameraInfo() may fail for inevitable hardware reasons.
// If the initialization failed for any (camera-related) reason, the camera gets
// completely unavailable.
throw new CameraUnavailableException("Failed to initialize the camera.", e);
}
}
protected CameraController instantiateCameraController(CameraCallbacks callbacks) {
return new Camera1(callbacks);

Loading…
Cancel
Save