sandrios / sandriosCamera

Easy integration of camera for image and video capturing with Image Picker.
MIT License
187 stars 54 forks source link

Crash when "Do not keep activities" is enabled #23

Closed joerandazzo closed 7 years ago

joerandazzo commented 7 years ago

Instructions to recreate:

  1. Enable 'Do not keep activities' in developer mode
  2. Launch camera, then press save on picture
  3. After hitting save, the following error occurs.

Process: com.coolfiresolutions.ronin.sa.base, PID: 17834 java.lang.NullPointerException: Attempt to invoke virtual method 'android.hardware.Camera$Parameters android.hardware.Camera.getParameters()' on a null object reference at com.sandrios.sandriosCamera.internal.manager.impl.Camera1Manager.getPhotoSizeForQuality(Camera1Manager.java:203)

I found that onPause is called in SandriosCameraActivity which calls onPause on cameraController which nulls out camera before Camera.getParameters is called

joerandazzo commented 7 years ago

@arpitgandhi9 Will this be resolved?

arpitgandhi9 commented 7 years ago

Values are passed via Intents. Even if catch the exceptions to stop the crash, we wont be able to use the library. Is there a specific reason for you to use "Do not keep activities"?

Also do you have any other suggestions than using activities. Was thinking of converting it all into fragments, but it is a long process that can take a lot of time.. Will definitely do it but for now can we go ahead with this?

mwshubham commented 7 years ago

How to Enable 'Do not keep activities' in developer mode? As i am facing the same error. Hope disabling them might give me more information related to issue.

Note: the same issue occur because of:

@Override
    public void openCamera(final Integer cameraId,
                           final CameraOpenListener<Integer, SurfaceHolder.Callback> cameraOpenListener) {
        this.currentCameraId = cameraId;
        backgroundHandler.post(new Runnable() {
            @Override
            public void run() {
                try {
                    camera = Camera.open(cameraId);
                    prepareCameraOutputs();
                    if (cameraOpenListener != null) {
                        uiHandler.post(new Runnable() {
                            @Override
                            public void run() {
                                cameraOpenListener.onCameraOpened(cameraId, previewSize, currentInstance);
                            }
                        });
                    }
                } catch (Exception error) {
                    Log.d(TAG, "Can't open camera: " + error.getMessage());
                    if (cameraOpenListener != null) {
                        uiHandler.post(new Runnable() {
                            @Override
                            public void run() {
                                cameraOpenListener.onCameraOpenError();
                            }
                        });
                    }
                }
            }
        });
    }

    @Override
    public void closeCamera(final CameraCloseListener<Integer> cameraCloseListener) {
        backgroundHandler.post(new Runnable() {
            @Override
            public void run() {
                if (camera != null) {
                    camera.release();
                    camera = null;
                    if (cameraCloseListener != null) {
                        uiHandler.post(new Runnable() {
                            @Override
                            public void run() {
                                cameraCloseListener.onCameraClosed(currentCameraId);
                            }
                        });
                    }
                }
            }
        });
    }

what if camera fragment is resumed and paused instantaneously (race around condition). Then, same error will be generated as camera is released at that time

 D/SandriosCameraFragment: onResume: 
 D/Camera1Controller: onResume: 
 D/Camera1Manager: openCamera: 
 D/SandriosCameraFragment: onPause: 
 D/Camera1Controller: onPause: 
 D/Camera1Manager: closeCamera: