7

My app is using camera. i'm having a CameraActivity class and CameraFragment class,

CameraFragment is responsible for releasing the camera:

@Override
public void onPause() {
    super.onPause();

    if (mCamera != null) {
        mCamera.release();
        mCamera = null;
    }
}

My app crashed while taking a picture. for some reason the camera was not released. now i can't get the camera object nowhere! Also the android Camera app is not working with an error "Can't connect to the camera"

now i know that the camera is a singleton object and only one app can get it in a time. what i don't understand is why onPause didn't run and how can i protect the camera instance better?

Also if something like that happens, how can i force release the camera object? it it a good idea?

sadly i don't have the information why the app crashed at the first place.

Thanks for your help.

Roy

royB
  • 12,779
  • 15
  • 58
  • 80
  • Well I'm guessing onPause didn't run cause it _crashed_. My guess is that the camera object _is_ released when your app crashes. – keyser May 03 '14 at 09:24
  • camera object is not released, that the main problem (and the weird part). camera object still was caught. the only way to release the camera object was to restart the device – royB May 03 '14 at 09:27
  • 2
    You're right, that can happen for some reason. [This](http://stackoverflow.com/a/17320457/645270) should help. – keyser May 03 '14 at 09:28
  • Thanks! using Thread.setDefaultUncaughtExceptionHandler, hope it will work – royB May 03 '14 at 09:41
  • This is a very valid issue - have been facing the same too... – slott Jul 25 '14 at 07:50

1 Answers1

0

This should help:

    private static void unCaughtExceptionHandler() {
    Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
        public void uncaughtException(final Thread thread, final Throwable ex) {
            ex.printStackTrace();
            releaseCamera();
            System.exit(0);
        }
    });
}
android_eng
  • 1,370
  • 3
  • 17
  • 40