3

I'm trying to create a Glass GDK app that uses the Camera service to show a preview. Unfortunately, I currently have a bug where a RuntimeException is thrown when trying to open a Camera using Camera.open(). I only encounter this bug when opening the activity through a voice trigger, not by selecting the app from the "launcher" menu.

Is there a difference in how an Activity is launched through this menu versus the voice trigger?

Some of the relevant code is below.

@Override
public void onCreate(Bundle savedInstanceState) {
    mGestureDetector = createGestureDetector(this);
    super.onCreate(savedInstanceState);
    ctx = this;
    act = this;
    setContentView(R.layout.activity_main);
    preview = new Preview(this, (SurfaceView)findViewById(R.id.surfaceView));
    ((FrameLayout) findViewById(R.id.preview)).addView(preview);
    preview.setKeepScreenOn(true);
}

@Override
protected void onResume() {
    super.onResume();
    try {
        if (camera == null) {
        Log.d(TAG, "Opening a camera on resume.");
        camera = Camera.open();
        preview.setCamera(camera);
        camera.startPreview();
        }
    } catch(java.lang.RuntimeException e) {
        Log.e(TAG, e.getMessage());
    }
}

@Override
protected void onPause() {
    if(camera != null) {
        camera.stopPreview();
        preview.setCamera(null);
        Log.d(TAG, "Releasing a camera on pause.");
        camera.release();
        camera = null;
    }
    super.onPause();
}

@Override
protected void onDestroy() {
    if(camera != null) {
        camera.stopPreview();
        preview.setCamera(null);
        Log.d(TAG, "Releasing a camera on destory.");
        camera.release();
        camera = null;
    }
    super.onDestroy();
}
Cole Gleason
  • 191
  • 4
  • 16

2 Answers2

3

Since it doesn't work when using the voice trigger, it sounds like a possible race condition where the microphone isn't released by the time your activity is displayed on the screen.

Can you try an approach that uses exponential back-off to capture the camera? Basically try to capture the camera and if you get an exception, try again after a short amount of time, increasing the wait time slightly for a fixed number of attempts.

Please also consider filing a bug on the issue tracker, especially if you can reliably find out how much of a delay is needed before the camera/mic can be acquired.

Tony Allevato
  • 6,429
  • 1
  • 29
  • 34
  • 1
    Thanks for the suggestion! I found that it usually took about 400ms for the Camera to be opened successfully. I have submitted an issue here: https://code.google.com/p/google-glass-api/issues/detail?id=259 – Cole Gleason Nov 23 '13 at 06:25
1

The problem is caused by the delay between the voice recogniser closing event and the camera open event, which is causing a memory overload. To avoid the problem when launching the app which will be triggered with voice, pause the app for certain time (1000 Milli seconds will do) from opening the camera soon.

In the below code I am delaying my QR scanner to open from opening for 1000 Milli seconds. This works fine for me. If you want a you can increase the time interval.

Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);

                intent = new Intent("com.google.zxing.client.android.SCAN");
                startActivityForResult(intent, 0);
            }
        };
        // sleeper time
        handler.sendEmptyMessageDelayed(0, 1000);  
Amalan Dhananjayan
  • 2,277
  • 1
  • 34
  • 47