RedApparat / Fotoapparat

Making Camera for Android more friendly. 📸
Apache License 2.0
3.82k stars 405 forks source link

io.fotoapparat.exception.camera.CameraException: Failed to open camera with lens position: io.fotoapparat.characteristic.LensPosition$Back@31c3b8a2 and id: 0 #311

Open devangichhatbar opened 5 years ago

devangichhatbar commented 5 years ago

When I open the first time It will work correctly. But after that, I click on other tabs and then open camera screen It will display a blank white screen.

I am using viewPager for that.

How did you initialize FA?

 mCameraView = view.findViewById(R.id.camera);

        fotoapparat = Fotoapparat.with(getActivity())
                .into(mCameraView)
                .previewScaleType(ScaleType.CenterCrop)
                .logger(new Logger() {
                    @Override
                    public void log(String s) {
                        Log.e("Camera", s);
                    }

                    @Override
                    public void recordMethod() {

                    }
                })
                .cameraErrorCallback(new CameraErrorListener() {
                    @Override
                    public void onError(CameraException e) {
                        e.printStackTrace();
                        fotoapparat.stop();
                    }
                })
                .build();
Pareshoct7 commented 5 years ago

Same issue.

wellbranding commented 5 years ago

Same issue. I have added runnable with delay 200 ms, when starting fotoappart camera. I am really sad that this library does not get any improvements...

selmantsn commented 5 years ago

Same issue. Is there any solution?

dineshvg commented 5 years ago

I made a workaround where we just restart the fragment or activity that holds the camera preview on the occurance of this error. It is shabby, but the user does not see a camera-view with an empty preview when the screen is opened after minimization.

ForestTree commented 5 years ago

Same problem, but ONLY on Huawei P9 Lite (Android 7.0). On the other tested devices, the problem does not occur (Huawei P8 Lite, Moto G4, Pixel 2). As mention @wellbranding , short delay before calling start() solved this issue.

DPalagi commented 5 years ago

Same issue here, only for activities where I force the requestedOrientation to landscape at start.

Even with the 200ms delay, it's not working...

wellbranding commented 5 years ago

@Diolor Same issue in 2.7.0. Should we stop fotoappart in onDestroyView or in onStop in the Fragment? What are your suggestions to avoid this problem? I have started to stop Fotoappart in onDestroyView and issue seems to be resolved, but I am still not sure. (We use this library in production app, so it is important for us to prevent this error :) )

Diolor commented 5 years ago

hey @wellbranding, do you still experience blank/black screens? Those should have been fixed in 2.7.0 due to a threading issue that we had.

To the lifecycle topic, well...there is no rule of the thumb. Depends your activities and how they work. Usually, start/stop is ok. Others can use resume/pause but camera will be paused in e.g. in split mode. I would avoid onDestroy because the activity GC is not guaranteed.

wellbranding commented 5 years ago

@Diolor blank/blacks screens seems to be fixed, indeed :) It works well with onStart and onStop with Activity. The problem occurs with Fragments.

Supppose I have a fragment, which will start an Activity, which has cameraSession (regular, not from fotoappart).

After onStop in that activity I experience same error here. We are still testing if it is our issue or not :) Maybe because we use Handler to stop fotoapparat, because otherwise it definitely causes issues, without Handler as was stated here: https://github.com/RedApparat/Fotoapparat/issues/311#issuecomment-432441965

Nevertheless, thanks for your recommendations! We will continue to use Fotoapparat in our SDK, which gets implemented across apps :)

Diolor commented 5 years ago

That's nice :) So, the hack of the 200ms should not be needed anymore. Give it a try and remove it, I would suggest.

Maybe some stop() operation is not executed somehow/sometime because of the handler or some other issue.

I would close issue this since blank screens are now gone 🎉

wellbranding commented 5 years ago

@Diolor yes, it seems to work well now! I create instance of Fotopparat inside of onActivityCreated in the fragment and then use onStop and onStart to handle stop() and start() accordingly.

wellbranding commented 5 years ago

@Diolor sadly, but issue continues.. Our testers team found that if you quickly open camera, then go to background, remove fragment and then will try to open fragment again you will encounter this error: Can't start preview because of the exception: java.io.IOException: setPreviewTexture failed which will then create a blank screen and will cause sameio.fotoapparat.characteristic.LensPosition$Back@31c3b8a2 and id: 0 #311

Seems not right :(

Diolor commented 5 years ago

A log would be a great help! 👍

wellbranding commented 5 years ago

@Diolor no problem, ask whatever you need :)

2019-03-15 20:15:07.308 14211-14368/com.idenfy.app.new D/Fotoapparat: CameraDevice: getPreviewResolution 2019-03-15 20:15:07.310 14211-14368/com.idenfy.app.new D/Fotoapparat: Preview resolution is: Resolution(width=1536, height=2048) 2019-03-15 20:15:07.311 14211-14368/com.idenfy.app.new D/Fotoapparat: CameraDevice: setDisplaySurface 2019-03-15 20:15:07.312 14211-14368/com.idenfy.app.new D/Fotoapparat: CameraDevice: startPreview 2019-03-15 20:15:08.279 14211-14211/com.idenfy.app.new D/Fotoapparat: Fotoapparat: stop 2019-03-15 20:15:08.283 14211-14368/com.idenfy.app.new D/Fotoapparat: CameraDevice: stopPreview 2019-03-15 20:15:08.324 14211-14368/com.idenfy.app.new D/Fotoapparat: CameraDevice: close 2019-03-15 20:15:09.268 14211-14211/com.idenfy.app.new D/Fotoapparat: Fotoapparat: start 2019-03-15 20:15:09.268 14211-14368/com.idenfy.app.new D/Fotoapparat: Device: selectCamera 2019-03-15 20:15:09.269 14211-14368/com.idenfy.app.new D/Fotoapparat: CameraDevice: open 2019-03-15 20:15:09.272 14211-14211/com.idenfy.app.new D/Fotoapparat: Fotoapparat: stop 2019-03-15 20:15:09.451 14211-14368/com.idenfy.app.new D/Fotoapparat: CameraDevice: getCapabilities$suspendImpl 2019-03-15 20:15:09.452 14211-14368/com.idenfy.app.new D/Fotoapparat: CameraDevice: updateParameters$suspendImpl 2019-03-15 20:15:09.453 14211-14368/com.idenfy.app.new D/Fotoapparat: New camera parameters are: CameraParameters flashMode: Flash.Off focusMode: FocusMode.Fixed jpegQuality: 90 exposureCompensation: 0 previewFpsRange: FpsRange(min=7500, max=24000) antiBandingMode: AntiBandingMode.Auto sensorSensitivity: null pictureResolution: Resolution(width=5184, height=3880) previewResolution: Resolution(width=2048, height=1536) 2019-03-15 20:15:09.461 14211-14368/com.idenfy.app.new D/Fotoapparat: CameraDevice: updateFrameProcessor 2019-03-15 20:15:09.464 14211-14368/com.idenfy.app.new D/Fotoapparat: CameraDevice: setDisplayOrientation 2019-03-15 20:15:09.465 14211-14368/com.idenfy.app.new D/Fotoapparat: Orientations: Screen orientation (preview) is: Orientation.Vertical.Portrait. Camera sensor orientation is always at: Orientation.Horizontal.ReverseLandscape. Camera is mirrored. 2019-03-15 20:15:09.465 14211-14368/com.idenfy.app.new D/Fotoapparat: Orientation adjustments: Image orientation will be adjusted by: 90 degrees. Display orientation will be adjusted by: 90 degrees. Preview orientation will be adjusted by: 90 degrees. 2019-03-15 20:15:09.465 14211-14368/com.idenfy.app.new D/Fotoapparat: CameraDevice: getPreviewResolution 2019-03-15 20:15:09.468 14211-14368/com.idenfy.app.new D/Fotoapparat: Preview resolution is: Resolution(width=1536, height=2048) 2019-03-15 20:15:09.468 14211-14368/com.idenfy.app.new D/Fotoapparat: CameraDevice: setDisplaySurface 2019-03-15 20:15:09.469 14211-14368/com.idenfy.app.new D/Fotoapparat: Can't start preview because of the exception: java.io.IOException: setPreviewTexture failed 2019-03-15 20:15:09.471 14211-14368/com.idenfy.app.new D/Fotoapparat: CameraDevice: stopPreview 2019-03-15 20:15:09.474 14211-14368/com.idenfy.app.new D/Fotoapparat: CameraDevice: close 2019-03-15 20:15:09.837 14211-14211/com.idenfy.app.new D/Fotoapparat: Fotoapparat: getCurrentParameters 2019-03-15 20:15:10.028 14211-14211/com.idenfy.app.new D/Fotoapparat: Device: updateLensPositionSelector 2019-03-15 20:15:10.029 14211-14211/com.idenfy.app.new D/Fotoapparat: Fotoapparat: 2019-03-15 20:15:10.031 14211-14211/com.idenfy.app.new D/Fotoapparat: Fotoapparat: start

In this case, error did not appeared, but we get a blank screen.

2019-03-15 20:19:16.088 14211-14211/com.idenfy.app.new D/Fotoapparat: Fotoapparat: stop 2019-03-15 20:19:16.089 14211-14946/com.idenfy.app.new D/Fotoapparat: Couldn't deliver pending result: Camera operation was cancelled. 2019-03-15 20:19:16.091 14211-14368/com.idenfy.app.new D/Fotoapparat: CameraDevice: stopPreview 2019-03-15 20:19:16.138 14211-14211/com.idenfy.app.new D/Fotoapparat: Device: updateLensPositionSelector 2019-03-15 20:19:16.138 14211-14368/com.idenfy.app.new D/Fotoapparat: CameraDevice: close 2019-03-15 20:19:16.139 14211-14211/com.idenfy.app.new D/Fotoapparat: Fotoapparat: 2019-03-15 20:19:16.139 14211-14211/com.idenfy.app.new D/Fotoapparat: Fotoapparat: start 2019-03-15 20:19:16.197 14211-14211/com.idenfy.app.new D/Fotoapparat: Fotoapparat: stop 2019-03-15 20:19:16.198 14211-14368/com.idenfy.app.new D/Fotoapparat: Device: selectCamera 2019-03-15 20:19:16.198 14211-14368/com.idenfy.app.new D/Fotoapparat: CameraDevice: open 2019-03-15 20:19:16.315 14211-14211/com.idenfy.app.new D/Fotoapparat: Device: updateLensPositionSelector 2019-03-15 20:19:16.316 14211-14211/com.idenfy.app.new D/Fotoapparat: Fotoapparat: 2019-03-15 20:19:16.317 14211-14211/com.idenfy.app.new D/Fotoapparat: Fotoapparat: start 2019-03-15 20:19:16.322 14211-14368/com.idenfy.app.new D/Fotoapparat: CameraDevice: getCapabilities$suspendImpl 2019-03-15 20:19:16.323 14211-14368/com.idenfy.app.new D/Fotoapparat: CameraDevice: updateParameters$suspendImpl 2019-03-15 20:19:16.323 14211-14368/com.idenfy.app.new D/Fotoapparat: New camera parameters are: CameraParameters flashMode: Flash.Off focusMode: FocusMode.Fixed jpegQuality: 90 exposureCompensation: 0 previewFpsRange: FpsRange(min=7500, max=24000) antiBandingMode: AntiBandingMode.Auto sensorSensitivity: null pictureResolution: Resolution(width=5184, height=3880) previewResolution: Resolution(width=2048, height=1536) 2019-03-15 20:19:16.338 14211-14368/com.idenfy.app.new D/Fotoapparat: CameraDevice: updateFrameProcessor 2019-03-15 20:19:16.343 14211-14368/com.idenfy.app.new D/Fotoapparat: CameraDevice: setDisplayOrientation 2019-03-15 20:19:16.343 14211-14368/com.idenfy.app.new D/Fotoapparat: Orientations: Screen orientation (preview) is: Orientation.Vertical.Portrait. Camera sensor orientation is always at: Orientation.Horizontal.ReverseLandscape. Camera is mirrored. 2019-03-15 20:19:16.343 14211-14368/com.idenfy.app.new D/Fotoapparat: Orientation adjustments: Image orientation will be adjusted by: 90 degrees. Display orientation will be adjusted by: 90 degrees. Preview orientation will be adjusted by: 90 degrees. 2019-03-15 20:19:16.343 14211-14368/com.idenfy.app.new D/Fotoapparat: CameraDevice: getPreviewResolution 2019-03-15 20:19:16.346 14211-14368/com.idenfy.app.new D/Fotoapparat: Preview resolution is: Resolution(width=1536, height=2048) 2019-03-15 20:19:16.346 14211-14368/com.idenfy.app.new D/Fotoapparat: CameraDevice: setDisplaySurface 2019-03-15 20:19:16.347 14211-14368/com.idenfy.app.new D/Fotoapparat: Can't start preview because of the exception: java.io.IOException: setPreviewTexture failed 2019-03-15 20:19:16.350 14211-14368/com.idenfy.app.new D/Fotoapparat: CameraDevice: stopPreview 2019-03-15 20:19:16.352 14211-14368/com.idenfy.app.new D/Fotoapparat: CameraDevice: close 2019-03-15 20:19:16.353 14211-14368/com.idenfy.app.new D/Fotoapparat: Device: selectCamera 2019-03-15 20:19:16.353 14211-14368/com.idenfy.app.new D/Fotoapparat: CameraDevice: open 2019-03-15 20:19:16.383 14211-14211/com.idenfy.app.new D/fotoapapartError: io.fotoapparat.exception.camera.CameraException: Failed to open camera with lens position: LensPosition.Back and id: 0 This time we get an exception.

Setup: Fragments, every fragment replaces other fragment, so onStop and onStart is called correctly

. In the parent activity of fragments inside of onStart, we remove some fragments (after returning from background)

I have looked internally Fotoapparat, because our team will also contribute to it soon, but I am not sure why error occurs.. We will try different setups and check how to possibly solve this issue. No memory leaks occur, it is well tested with LeakCanary.

Device Xioami mi A2, but occurs on emulators also

wellbranding commented 5 years ago

@Diolor does our logcat indicates possible issue with Fotoapparat? We have tested various ways. In the end, our results indicate if we call fotoapparat.start() in the onResume instead of onStart() in the fragment following exception does not occur:

Can't start preview because of the exception: java.io.IOException: setPreviewTexture failed

However, we do experience blank screen, with camera not opening. It could be because of fragments issues and different behavior: https://medium.com/androiddevelopers/the-android-lifecycle-cheat-sheet-part-iii-fragments-afc87d4f37fd

Diolor commented 5 years ago

setPreviewTexture is coming from the Android camera API. The documentation is incomplete but from my experience, this might happen if you have a view which has not been released from previous FA, or you try to reuse an existing view which is not "clean for usage" or because Android has a bad day...

I think this might have to do with object management with the lifecycle. E.g. first FA has not fully released camera and you instantly try to initiate a new one, or something wrong with the layout view.

wellbranding commented 5 years ago

@Diolor thanks for information. Yes it seems it is, because of those reasons. We will continue to use 2.7.0 version, but with Handlers when calling fotoapparat.start(). It passed our tests and seems to work well.

Replacing/ adding fragments with fotoappart object seems to be an issue..

adding/removing fragments works well without Handlers. I think that we should add an example,which would show proper use case with Fragments and launching Fotoapparat from them , because there are more issues with Fragments :)

arsenii-repository commented 5 years ago

Experiencing same issue with 2.7.0 version. Any news/workarounds?

arsenii-repository commented 5 years ago

@wellbranding Hi, may I ask you please to post working piece of code?

ErAmrit commented 5 years ago

Getting same issue on Moto G4 while using activity. When changing orientation black screen is occurred. Tried delay 200ms , also uses stop method before start. Please help version: 2.7.0

ErAmrit commented 5 years ago

When changing orientation black screen is occurred. Tried delay 300ms , also uses stop method before start. Continuously happening in Portrait if change from landscape version: 2.7.0

abayomiAkanji commented 4 years ago

Initialization of the object and calling fotoapparat.start() in the onResume method (of the fragment) seems to be the hack for me. This implies that whenever you return to the fragment, it re-initializes the object and starts it. This neither returns error nor does the camera goes blank.

N.B: Handler().postDelayed... is still used to start the object

noorashref commented 7 months ago

Im using 2.6.1 and got this exception mainly for moto G power (2021) Any solution for fix ? io.fotoapparat.exception.camera.CameraException : Failed to start preview for camera with lens position: io.fotoapparat.characteristic.LensPosition$Back@c1b6b84 and id: 0