Reputation: 209
I am having a application in which i am trying to draw some graph, For that i have used Surface View.. When i am clicking the "on" button my application has to open the Camera to fetch some parameters to draw some graph and Similarly when i will click the "off" button i need to stop the camera preview as well as drawing graph.. All this is working for a single attempt for me but when i am trying to repeat it then my application is crashing. And error shows in the
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
System.out.println("Deleted");
try { mCamera.setPreviewDisplay(sh);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace(); }
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
Here is my code....
public void startCamPreview() {
SurfaceView sv = (SurfaceView) findViewById(R.id.surfaceView);
final SurfaceHolder sh = sv.getHolder();
mCamera = Camera.open();
timer.start();
try {
mCamera.setPreviewDisplay(sh);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sh.addCallback(new Callback() {
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
System.out.println("Deleted");
try { mCamera.setPreviewDisplay(sh);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace(); }
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
System.out.println("Created");
mCamera.setPreviewCallback(new PreviewCallback() {
public void onPreviewFrame(byte[] data, Camera camera) {
Camera.Parameters params = camera.getParameters();
int width = params.getPreviewSize().width;
int height = params.getPreviewSize().height;
byte[] mYUVData = new byte[data.length];
int[] mRGBData = new int[width * height];
System.arraycopy(data, 0, mYUVData, 0, data.length);
decodeYUV420SP(mRGBData, mYUVData, width, height);
int[] mRedHistogram = new int[256];
calculateIntensityHistogram(mRGBData, mRedHistogram,
width, height, 0);
double imageRedMean = 0;
double redHistogramSum = 0;
for (int bin = 0; bin < 256; bin++) {
imageRedMean += mRedHistogram[bin] * bin;
redHistogramSum += mRedHistogram[bin];
} // bin
imageRedMean /= redHistogramSum;
double[] mBinSquared;
mBinSquared = new double[256];
for (int bin = 0; bin < 256; bin++) {
mBinSquared[bin] = ((double) bin) * bin;
}
double imageRed2ndMoment = 0;
for (int bin = 0; bin < 256; bin++) {
imageRed2ndMoment += mRedHistogram[bin]
* mBinSquared[bin];
} // bin
imageRed2ndMoment /= redHistogramSum;
double imageRedStdDev = Math.sqrt((imageRed2ndMoment
- imageRedMean * imageRedMean));
int val = (new Double(imageRedStdDev * 100000)
.intValue());
for (int l = 0; l < 1; l++) {
}
int yVal=250;
if (imageRedStdDev < 15 && val != 0)
{
mp.release();
show=true;
g.addPoint(yVal);
g.addPoint(yVal);
float flt=(float)Math.random();
g.addPoint(yVal+flt*25);
g.addPoint(yVal);
g.addPoint(yVal);
flt=(float)Math.random();
g.addPoint(yVal-flt*20);
g.addPoint(yVal);
g.addPoint(yVal);
flt=(float)Math.random();
g.addPoint(yVal-flt*30);
g.addPoint(yVal);
g.addPoint(yVal);
g.addPoint(yVal);
g.addPoint(yVal);
flt=(float)Math.random();
g.addPoint(yVal+flt*35);
mp=MediaPlayer.create(getApplicationContext(), R.raw.heartbeat);
try {
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.start();
//mp.setLooping(true);
Random r=new Random();
no=r.nextInt(84-60)+60;
bpmText_View.setText(String.valueOf(no));
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
show=false;
if(val==0){
count++;
g.addPoint(yVal);
g.addPoint(yVal);
g.addPoint(yVal);
g.addPoint(yVal);
mp.release();
mp=MediaPlayer.create(getApplicationContext(), R.raw.beep);
try {
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.start();
bpmText_View.setText(String.valueOf(0));
}
else{
mp.release();
count++;
g.addPoint(yVal);
g.addPoint(yVal);
g.addPoint(yVal);
g.addPoint(yVal);
mp=MediaPlayer.create(getApplicationContext(), R.raw.beep);
try {
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.start();
bpmText_View.setText(String.valueOf(0));
}
}
}
});
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format,
int width, int height) {
System.out.println("Changed");
Camera.Parameters parameters = mCamera.getParameters();
parameters.setPreviewSize(320, 240);
parameters.setPreviewFrameRate(15);
parameters.setSceneMode(Camera.Parameters.SCENE_MODE_NIGHT);
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
mCamera.setParameters(parameters);
mCamera.startPreview();
g.invalidate();
}
});
// fl.addView(b);
}
This is what my log cat showing
02-13 15:06:09.023: E/AndroidRuntime(13275): FATAL EXCEPTION: main 02-13 15:06:09.023: E/AndroidRuntime(13275): java.lang.RuntimeException: Method called after release() 02-13 15:06:09.023: E/AndroidRuntime(13275): at android.hardware.Camera.stopPreview(Native Method) 02-13 15:06:09.023: E/AndroidRuntime(13275): at com.yakshna.h2h.Graph$3.surfaceDestroyed(Graph.java:114) 02-13 15:06:09.023: E/AndroidRuntime(13275): at android.view.SurfaceView.reportSurfaceDestroyed(SurfaceView.java:587) 02-13 15:06:09.023: E/AndroidRuntime(13275): at android.view.SurfaceView.updateWindow(SurfaceView.java:481) 02-13 15:06:09.023: E/AndroidRuntime(13275): at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:213) 02-13 15:06:09.023: E/AndroidRuntime(13275): at android.view.View.dispatchDetachedFromWindow(View.java:6222) 02-13 15:06:09.023: E/AndroidRuntime(13275): at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1162) 02-13 15:06:09.023: E/AndroidRuntime(13275): at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1162) 02-13 15:06:09.023: E/AndroidRuntime(13275): at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1162) 02-13 15:06:09.023: E/AndroidRuntime(13275): at android.view.ViewRoot.dispatchDetachedFromWindow(ViewRoot.java:1751) 02-13 15:06:09.023: E/AndroidRuntime(13275): at android.view.ViewRoot.doDie(ViewRoot.java:2832) 02-13 15:06:09.023: E/AndroidRuntime(13275): at android.view.ViewRoot.die(ViewRoot.java:2802) 02-13 15:06:09.023: E/AndroidRuntime(13275): at android.view.WindowManagerImpl.removeViewImmediate(WindowManagerImpl.java:218) 02-13 15:06:09.023: E/AndroidRuntime(13275): at android.view.Window$LocalWindowManager.removeViewImmediate(Window.java:436) 02-13 15:06:09.023: E/AndroidRuntime(13275): at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:2705) 02-13 15:06:09.023: E/AndroidRuntime(13275): at android.app.ActivityThread.access$2100(ActivityThread.java:117) 02-13 15:06:09.023: E/AndroidRuntime(13275): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:968) 02-13 15:06:09.023: E/AndroidRuntime(13275): at android.os.Handler.dispatchMessage(Handler.java:99) 02-13 15:06:09.023: E/AndroidRuntime(13275): at android.os.Looper.loop(Looper.java:123) 02-13 15:06:09.023: E/AndroidRuntime(13275): at android.app.ActivityThread.main(ActivityThread.java:3687) 02-13 15:06:09.023: E/AndroidRuntime(13275): at java.lang.reflect.Method.invokeNative(Native Method) 02-13 15:06:09.023: E/AndroidRuntime(13275): at java.lang.reflect.Method.invoke(Method.java:507) 02-13 15:06:09.023: E/AndroidRuntime(13275): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842) 02-13 15:06:09.023: E/AndroidRuntime(13275): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 02-13 15:06:09.023: E/AndroidRuntime(13275): at dalvik.system.NativeStart.main(Native Method)
Upvotes: 0
Views: 332
Reputation: 56391
I agree that you need to post what the actual error is, but on first inspection, I'm betting it's a NullPointerException
.
My first guess is that you have a race condition. mCamera
is defined outside of your surfaceDestroyed
and startCamPreview
methods, but is referenced by both (including a closure on the Callback
anonymous class).
Upvotes: 0
Reputation: 10413
You mCamera.open() in a different Thread than you .startPreview():
Ref: http://developer.android.com/reference/android/hardware/Camera.html
"This class is not thread-safe, and is meant for use from one event thread. Most long-running operations (preview, focus, photo capture, etc) happen asynchronously and invoke callbacks as necessary. Callbacks will be invoked on the event thread open(int) was called from. This class's methods must never be called from multiple threads at once."
Upvotes: 1