florent37 / CameraFragment

A simple easy-to-integrate Camera Fragment for Android
Apache License 2.0
2.29k stars 421 forks source link

onPhotoTaken called twice when set from fragment #88

Closed einschneidend closed 6 years ago

einschneidend commented 6 years ago

On a Samsun S7, when setting the CameraFragmentResultListener in the cameraFragment causes it to onPhotoTaken to be called twice, setting the listener from takePhotoOrCaptureVideo (and removing it from cameraFragment) prevents/resolves this.

amadeu01 commented 6 years ago

Do you mean you have used those both ways?

cameraFragment.takePhotoOrCaptureVideo(
                    new CameraFragmentResultAdapter() {
                        @Override
                        public void onVideoRecorded(String filePath) {
                            Toast.makeText(getBaseContext(), "onVideoRecorded " + filePath, Toast.LENGTH_SHORT).show();
                        }

                        @Override
                        public void onPhotoTaken(byte[] bytes, String filePath) {
                            Toast.makeText(getBaseContext(), "onPhotoTaken " + filePath, Toast.LENGTH_SHORT).show();

                        }
                    },
                    String.valueOf(Environment.getExternalStorageDirectory()),
                    "photo0" + new Random().nextLong());
        }

and you have used

cameraFragment.setResultListener(new CameraFragmentResultListener() {
                @Override
                public void onVideoRecorded(String filePath) {
                    Intent intent = PreviewActivity.newIntentVideo(MainActivity.this, filePath);
                    startActivityForResult(intent, REQUEST_PREVIEW_CODE);
                }

                @Override
                public void onPhotoTaken(byte[] bytes, String filePath) {

                }
            });

I guess it will call both of them, cause you are subscribing to both events.

polaris0227 commented 6 years ago

You have to make some changes in BaseAnncaFragment.java It calls CameraFragmentResultListener twice.

Origin code of onCreate():

            ...
            @Override
            public void onPhotoTaken(byte[] bytes, CameraFragmentResultListener callback) {
                final String filePath = cameraController.getOutputFile().toString();
                if (cameraFragmentResultListener != null) {
                    cameraFragmentResultListener.onPhotoTaken(bytes, filePath);
                }
                if (callback != null) {
                    callback.onPhotoTaken(bytes, filePath);
                }
            }
            ...

Here is the code changed:

            ...
            @Override
            public void onPhotoTaken(byte[] bytes, CameraFragmentResultListener callback) {
                final String filePath = cameraController.getOutputFile().toString();
                if (cameraFragmentResultListener != null) {
                    cameraFragmentResultListener.onPhotoTaken(bytes, filePath);
                }
                // if (callback != null) {
                //     callback.onPhotoTaken(bytes, filePath);
                // }
            }
            ...

in above code, cameraFragmentResultListener and callback is the same CameraFragmentResultListener . So you need to use one of them. Hope it can help someone.