Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix mutiple getParameters() unhandled RuntimeException
Every time getParameters() is called, it can fail with RuntimeException: "getParameters failed (empty parameters)"

So the fix is to try/catch and Log.e the failure. Please tell me if you need to throw REASON_UNLNOWN exception or let the app continue.

LOGCAT CRASH:

Exception java.lang.RuntimeException: getParameters failed (empty parameters)
  at android.hardware.Camera.native_getParameters
  at android.hardware.Camera.getParameters (Camera.java:3088)
  at com.otaliastudios.cameraview.engine.Camera1Engine$3.run (Camera1Engine.java:579)
  at com.otaliastudios.cameraview.engine.orchestrator.CameraStateOrchestrator$3.run (CameraStateOrchestrator.java:100)
  at com.otaliastudios.cameraview.engine.orchestrator.CameraOrchestrator$1.call (CameraOrchestrator.java:84)
  at com.otaliastudios.cameraview.engine.orchestrator.CameraOrchestrator$1.call (CameraOrchestrator.java:81)
  at com.otaliastudios.cameraview.engine.orchestrator.CameraOrchestrator$3.run (CameraOrchestrator.java:152)
  at com.otaliastudios.cameraview.internal.WorkerHandler.run (WorkerHandler.java:137)
  at com.otaliastudios.cameraview.engine.orchestrator.CameraOrchestrator.execute (CameraOrchestrator.java:147)
  at com.otaliastudios.cameraview.engine.orchestrator.CameraOrchestrator.access$100 (CameraOrchestrator.java:34)
  at com.otaliastudios.cameraview.engine.orchestrator.CameraOrchestrator$2.run (CameraOrchestrator.java:137)
  at android.os.Handler.handleCallback (Handler.java:836)
  at android.os.Handler.dispatchMessage (Handler.java:103)
  at android.os.Looper.loop (Looper.java:203)
  at android.os.HandlerThread.run (HandlerThread.java:61)
  • Loading branch information
EzequielAdrianM authored Jan 7, 2024
commit 3db652d4d298de18bd971de7f78d4f41c3ef72fc
Original file line number Diff line number Diff line change
Expand Up @@ -528,8 +528,14 @@ public void setFlash(@NonNull Flash flash) {
new Runnable() {
@Override
public void run() {
Camera.Parameters params = mCamera.getParameters();
if (applyFlash(params, old)) mCamera.setParameters(params);
try {
Camera.Parameters params = mCamera.getParameters();
if (applyFlash(params, old)) mCamera.setParameters(params);
} catch (RuntimeException e) {
LOG.e("onSetFlash:", "Failed to get params from camera. Maybe low level problem with camera or camera has already released?");
//Should throw exception? Looks inappropriate at this point if camera is working...
//throw new CameraException(e, CameraException.REASON_FAILED_TO_START_PREVIEW);
}
}
});
}
Expand All @@ -552,8 +558,14 @@ public void setLocation(@Nullable Location location) {
new Runnable() {
@Override
public void run() {
Camera.Parameters params = mCamera.getParameters();
if (applyLocation(params, oldLocation)) mCamera.setParameters(params);
try {
Camera.Parameters params = mCamera.getParameters();
if (applyLocation(params, oldLocation)) mCamera.setParameters(params);
} catch (RuntimeException e) {
LOG.e("onSetLocation:", "Failed to get params from camera. Maybe low level problem with camera or camera has already released?");
//Should throw exception? Looks inappropriate at this point if camera is working...
//throw new CameraException(e, CameraException.REASON_FAILED_TO_START_PREVIEW);
}
}
});
}
Expand All @@ -580,8 +592,14 @@ public void setWhiteBalance(@NonNull WhiteBalance whiteBalance) {
new Runnable() {
@Override
public void run() {
Camera.Parameters params = mCamera.getParameters();
if (applyWhiteBalance(params, old)) mCamera.setParameters(params);
try {
Camera.Parameters params = mCamera.getParameters();
if (applyWhiteBalance(params, old)) mCamera.setParameters(params);
} catch (RuntimeException e) {
LOG.e("onSetWhiteBalance:", "Failed to get params from camera. Maybe low level problem with camera or camera has already released?");
//Should throw exception? Looks inappropriate at this point if camera is working...
//throw new CameraException(e, CameraException.REASON_FAILED_TO_START_PREVIEW);
}
}
});
}
Expand Down Expand Up @@ -609,8 +627,14 @@ public void setHdr(@NonNull Hdr hdr) {
new Runnable() {
@Override
public void run() {
Camera.Parameters params = mCamera.getParameters();
if (applyHdr(params, old)) mCamera.setParameters(params);
try {
Camera.Parameters params = mCamera.getParameters();
if (applyHdr(params, old)) mCamera.setParameters(params);
} catch (RuntimeException e) {
LOG.e("onSetHdr:", "Failed to get params from camera. Maybe low level problem with camera or camera has already released?");
//Should throw exception? Looks inappropriate at this point if camera is working...
//throw new CameraException(e, CameraException.REASON_FAILED_TO_START_PREVIEW);
}
}
});
}
Expand All @@ -635,12 +659,18 @@ public void setZoom(final float zoom, @Nullable final PointF[] points, final boo
new Runnable() {
@Override
public void run() {
Camera.Parameters params = mCamera.getParameters();
if (applyZoom(params, old)) {
mCamera.setParameters(params);
if (notify) {
getCallback().dispatchOnZoomChanged(mZoomValue, points);
try {
Camera.Parameters params = mCamera.getParameters();
if (applyZoom(params, old)) {
mCamera.setParameters(params);
if (notify) {
getCallback().dispatchOnZoomChanged(mZoomValue, points);
}
}
} catch (RuntimeException e) {
LOG.e("onSetZoom:", "Failed to get params from camera. Maybe low level problem with camera or camera has already released?");
//Should throw exception? Looks inappropriate at this point if camera is working...
//throw new CameraException(e, CameraException.REASON_FAILED_TO_START_PREVIEW);
}
}
});
Expand Down Expand Up @@ -670,13 +700,19 @@ public void setExposureCorrection(final float EVvalue, @NonNull final float[] bo
new Runnable() {
@Override
public void run() {
Camera.Parameters params = mCamera.getParameters();
if (applyExposureCorrection(params, old)) {
mCamera.setParameters(params);
if (notify) {
getCallback().dispatchOnExposureCorrectionChanged(mExposureCorrectionValue,
bounds, points);
try {
Camera.Parameters params = mCamera.getParameters();
if (applyExposureCorrection(params, old)) {
mCamera.setParameters(params);
if (notify) {
getCallback().dispatchOnExposureCorrectionChanged(mExposureCorrectionValue,
bounds, points);
}
}
} catch (RuntimeException e) {
LOG.e("onSetExposureCorrection:", "Failed to get params from camera. Maybe low level problem with camera or camera has already released?");
//Should throw exception? Looks inappropriate at this point if camera is working...
//throw new CameraException(e, CameraException.REASON_FAILED_TO_START_PREVIEW);
}
}
});
Expand Down Expand Up @@ -748,8 +784,14 @@ public void setPreviewFrameRate(float previewFrameRate) {
new Runnable() {
@Override
public void run() {
Camera.Parameters params = mCamera.getParameters();
if (applyPreviewFrameRate(params, old)) mCamera.setParameters(params);
try {
Camera.Parameters params = mCamera.getParameters();
if (applyPreviewFrameRate(params, old)) mCamera.setParameters(params);
} catch (RuntimeException e) {
LOG.e("onSetPreviewFrameRate:", "Failed to get params from camera. Maybe low level problem with camera or camera has already released?");
//Should throw exception? Looks inappropriate at this point if camera is working...
//throw new CameraException(e, CameraException.REASON_FAILED_TO_START_PREVIEW);
}
}
});
}
Expand Down Expand Up @@ -879,7 +921,13 @@ public void run() {
getPreview().getSurfaceSize());
MeteringRegions transformed = regions.transform(transform);

Camera.Parameters params = mCamera.getParameters();
Camera.Parameters params;
try {
params = mCamera.getParameters();
} catch (RuntimeException re) {
LOG.e("startAutoFocus:", "Failed to get camera parameters");
throw new CameraException(re, CameraException.REASON_UNKNOWN);
}
int maxAF = params.getMaxNumFocusAreas();
int maxAE = params.getMaxNumMeteringAreas();
if (maxAF > 0) params.setFocusAreas(transformed.get(maxAF, transform));
Expand Down