public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
..........
.......
public void surfaceCreated(SurfaceHolder holder) {
........
..........
// I create camera object with camera.open
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
// I start camera preview successfully and call my function.
Myfunction();
//this function does some checking (a separate thread ) n releases camera object immediately and also release the camera preview
}
}
Now that I am still in onsurfaceChanged()
when the control leaves this function I get a Force close(Null Pointer exception
) at SurfaceView (564)
. I know this error is because i released the camera and its suface view. According to the documentation of android the onsurfaceChanged()
is called atleast once after surfaceCreated();
So my Query is it wrong that i am releasing the camera object and its preview holder before the control goes out of onsurfaceChanged()
?? My requirement is I check some situation n release the camera if its condition is false as soon as create preview i.e startPreview();
do some task ahead inside onsurfaceChanged()
itself.
is there any way i can detect the onsurfaceChanged()
is called once and then i can trigger Myfunction();
The easy workaround would be to use CameraPreview.post()
to release camera after your callback returns:
public class CameraPreview … {
void Myfunction() {
…
this.post(new Runnable() {
public void run() {
mCamera.release();
mCamera = null;
}
}
…
}
}