locked
PhotoCamera dispose exception RRS feed

  • Question

  • Hi,

    I'm using PhotoCamera class the get preview image and I got some problem while Dispose the camera.

    The reproduce procedure:

    1. Launch the application and press "windows key" to exit page and back to idle immediately

    2. Press "back" key to resume application

    repeat 1 and 2 and app will crash for below reason

    System.ObjectDisposedException

    The object has been closed.
       at Windows.Phone.Media.Capture.PhotoCaptureDevice.GetProperty(Guid propertyId)
       at Microsoft.Devices.Camera.GetVideoPortName()
       at Microsoft.Devices.Camera.<>c__DisplayClass2.<InitializeVideoSession>b__1()

    My code is below

    In MainPage.xaml.cs

    protected override void OnNavigatedFrom(NavigationEventArgs aNavigationEventArgs)
    {
      DisposeCameraDevices();
      base.OnNavigatedFrom(aNavigationEventArgs);
    }
    protected override void OnNavigatedTo(NavigationEventArgs aNavigationEventArgs)
    {
        base.OnNavigatedTo(aNavigationEventArgs);
        OpenCameraDevices();
    }
    public void OpenCameraDevices()
    {
      try
      {
        mCamera = new PhotoCamera(CameraType.Primary);
        mCamera.Initialized += CameraInitialized;
        viewfinderBrush.SetSource(mCamera);
      }
      catch (Exception ex)
      {
      }
    }
    private void CameraInitialized(object aSender, CameraOperationCompletedEventArgs aCameraOperationCompletedEventArgs)
    {
      if (aCameraOperationCompletedEventArgs.Succeeded)
      {
         
      }
    }
    public void DisposeCameraDevices()
    {
      try
      {
        if (mCamera != null)
        {
          mCamera.Dispose();
          mCamera.Initialized -= CameraInitialized;
        }
      }
      catch (Exception ex)
      {
      }
    }

    My develop target is windows phone 8. Verify devices are Nokia Lumia920 and HTC 8X

    Thanks.!

    Wednesday, December 5, 2012 3:24 AM

All replies

  • i have the same situation too
    Wednesday, January 16, 2013 5:12 AM
  • Were you able to resolve it? I am facing the same issue.

    Monday, February 11, 2013 3:26 PM
  • Hi, 

    I think this problem occurs when we dispose the camera before it gets initialized. I took the following steps:-

    PhotoCamera cam;
    cam = new Microsoft.Devices.PhotoCamera();
    cam.Initialized += new EventHandler<CameraOperationCompletedEventArgs>(cam_Initialized);
    viewfinderBrush.SetSource(cam);

    In the Initialized event:-

    void cam_Initialized(object sender, CameraOperationCompletedEventArgs e)
            {
                flag= true;  
            }
    

    Then in the event where you are going back,check if the cam is initialized or not.If the flag is true then continue with next function. Else cancel the next function.

    For example: On back key press it looks as follows:-

    protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
            {
                if (flag== false )
                        {
                            // If the Camera is not initialised, cancel the back key press.
                            e.Cancel = true;
    
                         }
             }
    

    Friday, February 22, 2013 10:19 AM
  • i have the same situation too

     Launch the PhotoCamera application and press "windows start or search key" to exit page and back to idle immediately

    Friday, May 24, 2013 3:30 AM
  • this only work on back key press ; Launch the PhotoCamera application and press "windows start or search key" to exit page and back to idle immediately  occur PhotoCamera dispose exception

    how to resolve  start or search key press? 

    Friday, May 24, 2013 3:33 AM
  • any success with this exception? I have the same pronlem and do not know how to fix it
    Monday, September 9, 2013 1:39 PM
  • Hi, does anybody has a solution? I have the same problem.


    И таки, что бы вы подумали? Снизошло... (c)Bondor

    Tuesday, July 22, 2014 3:54 PM
  • I suspected the same issue in my application, the provided solution works for me.
    Wednesday, August 13, 2014 6:33 PM
  • Yes, it solution resolved this problem, but not completely. When start key pressed, camera disposing incorrectly and app can't resume.

    И таки, что бы вы подумали? Снизошло... (c)Bondor

    Wednesday, August 13, 2014 6:41 PM
  • Yes, it solution resolved this problem, but not completely. When start key pressed, camera disposing incorrectly and app can't resume.

    И таки, что бы вы подумали? Снизошло... (c)Bondor

    Yes, I see what you mean.  Use IsNavigationInitiator flag from the NavigationCancelEventArgs object.  Here is a solution to handle pressing the start key as well  as the search key in conjunction with the method suggested by Swetha .  In the method ReleaseCamera(), I am calling the camera's dispose method.  The cameraready flag is set in the camera initialized event handler.

            protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
            {
                base.OnNavigatingFrom(e);
    
                if (this.cameraready == false)
                {
                    if (e.IsNavigationInitiator == true)
                    {
                        e.Cancel = true;
                    }
                    else
                    {
                        if (this.mycamera != null)
                        {
                            this.mycamera.ReleaseCamera();
                        }
                    }
                }
                else
                {
                    if (this.mycamera != null)
                    {
                        this.mycamera.ReleaseCamera();
                    }
                }
            }

    • Proposed as answer by Jose1966 Monday, September 22, 2014 1:53 AM
    Monday, August 18, 2014 3:52 PM