Reputation: 38595
I'm working with the CameraPreview in the API Demos that comes with the Android SDK. While the camera preview is running, if I turn off my screen and turn it on again, the camera preview is completely black. The device's camera application somehow manages to restore the camera preview, so there must be a way.
Not all devices I've tried exhibit this behavior. I can't confirm, but it seems like the OS version matters.
Upvotes: 1
Views: 3073
Reputation: 1
My solution is release camera in surfaceDestroyed method not in onPause method and restart camera in surfaceCreated method .The result is fine.
Upvotes: 0
Reputation: 16832
What helped me was 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.
Upvotes: 1
Reputation: 91
I had the same problem. Maybe, it isn't the best solution, but it works for me. In onPause() method restart your camera activity.
private boolean isBackPreesed = false;
@Override
public void onPause() {
if (camera != null) {
camera.release();
camera = null;
}
if (!isBackPreesed) {
finish();
Intent restart = new Intent(this, this.getClass());
startActivity(restart);
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
isBackPreesed = true;
}
return super.onKeyDown(keyCode, event);
}
Upvotes: 0
Reputation: 93561
You need to release and reacquire the camera in pause/resume. Here's some code from my CameraView widget:
public void onPause(){
if(camera != null){
camera.release();
camera = null;
}
}
public void onResume(){
//Need to release if we already have one, or we won't get the camera
if(camera != null){
camera.release();
camera = null;
}
try {
camera = Camera.open();
}
catch (Exception e){
}
}
Upvotes: 1