Commonsware camera preview demo: no preview after device on/off

538 Views Asked by At

I see a bug: when the camera is in preview mode, and the user presses on/off, the camera preview stops and does not start.

An obvious suggestion is to see what other people do; I have consulted https://github.com/commonsguy/cw-advandroid/tree/master/Camera/Preview/ and I see that this demo has the same bug (Android 4.0.3).

A direct link to the java source: https://github.com/commonsguy/cw-advandroid/blob/master/Camera/Preview/src/com/commonsware/android/camera/PreviewDemo.java

QUESTION: how can one fix the bug? The preview mode must be working even after device on/off.

UPD: The bug does not show if I first change to another Activity and press On/Off on that Activity.

UPD2: After on/off, startPreview() is invoked, but I see a message from the system:

D/@@    (13428): startPreview() cameraConfigured=true camera=android.hardware.Camera@412f66f0
E/CamHw ( 2135): preview window is NULL!

which means that something bad does happen in depths of the system (although the message may be device-specific).

UDP3: after on/off, no method from surfaceCallback is invoked. Maybe this makes the difference.

UPD4: [seen not exactly at this camera demo] if you switch to a different application (by long-pressing HOME and choosing from the list) and then switch back, the camera works again.

1

There are 1 best solutions below

0
On

It is not a solution, but a workaround: invoke setContentView() in onResume().

It can be either

protected void onResume() {
    super.onResume();
    setContentView(R.layout.xxx);
    // ...
}

or

private View cachedContentView;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    cachedContentView = doCreateContentView(getLayoutInflater());
    // ...
}
protected void onResume() {
    super.onResume();
    setContentView(cachedContentView);
    // ...
}

Both of them work.

I still have no idea what causes the problem and what is the difference between the original example and the library.