I am working on a custom camera app, everything works fine. But, recently I have discovered that my application does not behave the same on some devices.
At the end of my camera activity i tend to release the camera and stop the preview.
After many researches, I found that the method that does the release of the camera is not working on the devices that have a build version greater than 2.6.x. The tests were realized on several devices and several android versions. The issue comes from the build version which seems to not stop the camera when it executes the "camera.release()" instruction.
Here's my code that works perfectly on any other device that has a build version =< 2.6.x PS: I am using Camera.Hardware library that I know is deprecated.
private void stopPreview () {
if ( camera != null) {
if (camera != null) {
camera.release();
}
this.inPreview = false;
cameraReleased = true;
onAutofocus = false;
}
}
Can anyone help me please understand why camera.release() does not release the camera on those specific build versions?
I temporarely fixed my issue by stopping the preview before releasing the camera :
private void stopPreview() {
if (camera != null) {
try {
// Stop preview first
camera.stopPreview();
// Detach preview display (optional but safer)
camera.setPreviewCallback(null);
camera.setPreviewDisplay(null);
} catch (Exception e) {
Log.w("CameraRelease", "Error while stopping preview: " + e.getMessage());
}
// Finally release the camera
camera.release();
camera = null;
inPreview = false;
cameraReleased = true;
onAutofocus = false;
}
}
Finally, i migrated all camera.Hardware usages to CameraX