setParameters exception... not able to use Camera
Hi guys I am using the following code to access Camera from my application :-
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
isPreviewRunning = false;
surface = (SurfaceView)findViewById(R.id.surface);
holder = surface.getHolder();
holder.addCallback(this);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
if (isPreviewRunning) {
camera.stopPreview();
}
Camera.Parameters p = camera.getParameters();
p.setPreviewSize(480, 800);
p.setPictureFormat(PixelFormat.JPEG);
camera.setParameters(p);
try {
camera.setPreviewDisplay(holder);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
camera.startPreview();
isPreviewRunning=true;
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
camera = Camera.open();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
camera.stopPreview();
camera.release();
camera=null;
}
I get force close and the error in log cat is following :-
04-13 15:21:36.979: ERROR/AndroidRuntime(14583): FATAL EXCEPTION: main
04-13 15:21:36.979: ERROR/AndroidRuntime(14583): java.lang.RuntimeException: setParameters failed
04-13 15:21:36.979: ERROR/AndroidRuntime(14583): at android.hardware.Camera.native_setParameters(Native Method)
04-13 15:21:36.979: ERROR/AndroidRuntime(14583): at android.hardware.Camera.setParameters(Camera.java:655)
04-13 15:21:36.979: ERROR/AndroidRuntime(14583): at org.mycamera.mycamera.surfaceChanged(mycamera.java:44)
04-13 15:21:36.979: ERROR/AndroidRuntime(14583): at android.view.SurfaceView.updateWindow(SurfaceView.java:546)
04-13 15:21:36.979: ERROR/AndroidRuntime(14583): at android.view.SurfaceView.dispatchDraw(SurfaceView.java:339)
04-13 15:21:36.979: ERROR/AndroidRuntime(14583): at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
04-13 15:21:36.979: ERROR/AndroidRuntime(14583): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375)
04-13 15:21:36.979: ERROR/AndroidRuntime(14583): at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
04-13 15:21:36.979: ERROR/AndroidRuntime(14583): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375)
04-13 15:21:36.979: ERROR/AndroidRuntime(14583): at android.view.View.draw(View.java:6742)
04-13 15:21:36.979: ERROR/AndroidRuntime(14583): at android.widget.FrameLayout.draw(FrameLayout.java:352)
04-13 15:21:36.979: ERROR/AndroidRuntime(14583): at android.view.ViewGroup.drawChild(ViewGroup.java:1648)
04-13 15:21:36.979: ERROR/AndroidRuntime(14583): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375)
04-13 15:21:36.979: ERROR/AndroidRuntime(14583): at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
04-13 15:21:36.979: ERROR/AndroidRuntime(14583): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375)
04-13 15:21:36.979: ERROR/AndroidRuntime(14583): at android.view.View.draw(View.java:6742)
04-13 15:21:36.979: ERROR/AndroidRuntime(14583): at android.widget.FrameLayout.draw(FrameLayout.java:352)
04-13 15:21:36.979: ERROR/AndroidRuntime(14583): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1872)
04-13 15:21:36.979: ERROR/AndroidRuntime(14583): at andr开发者_StackOverflowoid.view.ViewRoot.draw(ViewRoot.java:1422)
04-13 15:21:36.979: ERROR/AndroidRuntime(14583): at android.view.ViewRoot.performTraversals(ViewRoot.java:1167)
04-13 15:21:36.979: ERROR/AndroidRuntime(14583): at android.view.ViewRoot.handleMessage(ViewRoot.java:1744)
04-13 15:21:36.979: ERROR/AndroidRuntime(14583): at android.os.Handler.dispatchMessage(Handler.java:99)
04-13 15:21:36.979: ERROR/AndroidRuntime(14583): at android.os.Looper.loop(Looper.java:144)
04-13 15:21:36.979: ERROR/AndroidRuntime(14583): at android.app.ActivityThread.main(ActivityThread.java:4937)
04-13 15:21:36.979: ERROR/AndroidRuntime(14583): at java.lang.reflect.Method.invokeNative(Native Method)
04-13 15:21:36.979: ERROR/AndroidRuntime(14583): at java.lang.reflect.Method.invoke(Method.java:521)
04-13 15:21:36.979: ERROR/AndroidRuntime(14583): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-13 15:21:36.979: ERROR/AndroidRuntime(14583): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-13 15:21:36.979: ERROR/AndroidRuntime(14583): at dalvik.system.NativeStart.main(Native Method)
You will have to use one of the supported picture sizes:
Camera.Parameters p = camera.getParameters();
List<Size> sizes = p.getSupportedPictureSizes();
// Choose any one you want among sizes
size = sizes.get(0);
p.setPictureSize(size.width, size.height);
camera.setParameters(p);
精彩评论