Skip to content

Commit

Permalink
fix(android)(9_0_X): autoFocus causing crash on some versions of Andr…
Browse files Browse the repository at this point in the history
…oid (#11562)

* fix(android): autoFocus causing crash on some versions of Android

Crash occurs on some devices running Android versions 5.01, 5.1 and 6.0.1 when taking a picture.

* fix(android): amend auto focus callback

Co-authored-by: Scott Null <scott.null@digi.com>
  • Loading branch information
garymathews and Scott Null committed Mar 26, 2020
1 parent 5a5c0f5 commit ab3d8c6
Showing 1 changed file with 20 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -716,31 +716,36 @@ static public void takePicture()
public void onAutoFocus(boolean success, Camera camera)
{
if (takingPicture) {
try {
camera.takePicture(shutterCallback, null, jpegCallback);
} catch (Exception e) {
Log.w(TAG, "could not take picture: " + e.toString());
takingPicture = false;
}
if (!success) {
if (success) {
try {
camera.takePicture(shutterCallback, null, jpegCallback);
} catch (Exception e) {
Log.w(TAG, "Could not take picture: " + e.toString());
takingPicture = false;
}

// This is required in order to continue auto-focus after taking a picture.
// Calling 'cancelAutofocus' may cause issues on Android M. (TIMOB-20260)
// Which is why this requires an exception handler.
// NOTE: We should really update to Camera2 API.
try {
camera.cancelAutoFocus();
camera.autoFocus(null);
} catch (Exception e) {
Log.w(TAG, "Failed to cancel auto focus: " + e.toString());
}
} else {
Log.w(TAG, "Unable to focus.");
}
}
// This is a Hotfix for TIMOB-20260
// "cancelAutoFocus" causes the camera to crash on M (probably due to discontinued support of android.hardware.camera)
// We need to move to android.hardware.camera2 APIs as soon as we can.
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
camera.cancelAutoFocus();
}
camera.autoFocus(null);
}
};
camera.autoFocus(focusCallback);
} else {
camera.takePicture(shutterCallback, null, jpegCallback);
}
} catch (Exception e) {
Log.w(TAG, "could not take picture: " + e.toString());
Log.w(TAG, "Could not take picture: " + e.toString());
if (camera != null) {
camera.release();
}
Expand Down

0 comments on commit ab3d8c6

Please sign in to comment.