Hi I am developing my app with Camera functionality with SurfaceView. I want to take picture when surface view created.
In my fragment's onCreateView method, initialized everything what I need.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.game_one_start_fragment, container, false);
initWidgets(view);
return view;
}
In initWidgets() method initialized the SurfaceView object as below
mSurfaceView = (SurfaceView) view.findViewById(R.id.game_action_surfaceview);
mSurfaceHolder = mSurfaceView.getHolder();
In OnResume() method getting the surfaceHolder object.
@Override
public void onResume() {
super.onResume();
mSurfaceHolder.addCallback(this);
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
safeCameraOpen(Camera.CameraInfo.CAMERA_FACING_BACK);
}
safeCameraOpen() I have followed the Android Developer link. Used the same code as mentioned.
The surfaceHolder.callback used as below
@Override
public void surfaceCreated(SurfaceHolder holder) {
mCamera.startPreview();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
try {
//to set the picture orientation
setCameraDisplayOrientation(getActivity(), findFrontFacingCameraID(), mCamera);
mCamera.startPreview();
mCamera.setPreviewDisplay(holder);
} catch (IOException e) {
e.printStackTrace();
}
Camera.Parameters parameters = mCamera.getParameters();
parameters.getSupportedPreviewSizes();
mCamera.setParameters(parameters);
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
mCamera.stopPreview();
mCamera.release();
}
I want to take picture when the fragment loaded.
I have tried in OnViewCreated(),onResume() and onStart() lifecycle methods. Within them I have used the following line
mCamera.takePicture(null,null,mPictureCallback);
(mCamera object created and initialized in safeCameraOpen() method)
If I tried within the mentioned lifecycle methods, I am getting the following crash error.
java.lang.RuntimeException: takePicture failed
at android.hardware.Camera.native_takePicture(Native Method)
at android.hardware.Camera.takePicture(Camera.java:1266)
at android.hardware.Camera.takePicture(Camera.java:1211)
at com.gaming.fragment.GameOneStartFragment.snapPicture(GameOneStartFragment.java:76)
at com.gaming.fragment.GameOneStartFragment.surfaceCreated(GameOneStartFragment.java:284)
at android.view.SurfaceView.updateWindow(SurfaceView.java:572)
at android.view.SurfaceView.access$000(SurfaceView.java:86)
at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:175)
at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:847)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1897)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1019)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5725)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:544)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5086)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
But, when I use the takePicture() method of Camera object inside a button click it capture the picture.
How can I do take picture without view click ?
You don't call the startPreview method of Camera object. I think you should call startPreview before take a picture. In this case you didn't set the preview display:
Check this answer: How to take pictures from the camera without preview when my app starts?