0

Hi,

I have created a fragment that contains code for a camera preview. I am now trying to create an activity that will look after taking the picture etc..

Here is my cam preview related code from my fragment (this works just fine)

preview = (SurfaceView) view.findViewById(R.id.preview);
                previewHolder = preview.getHolder();
                previewHolder.addCallback(surfaceCallback);
                previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

                    @Override
        public void onStop() {
            //Unregistering the Listener
            manager.unregisterListener(this);
            super.onStop();

        }

        @Override
        public void onPause() {

            super.onPause();
            if(camera!=null)
            {
                camera.stopPreview();

            }
            //On Entering the Pause Method, if in Preview - Stop
            if (inPreview) {
                camera.stopPreview();
            }

            //Release the Camera and Set the Camera and InPreview to Negative
            camera.release();
            camera = null;
            inPreview = false;

        }



        //Method to Determine the Best Preview Size
        private Camera.Size getBestPreviewSize(int width, int height,
                Camera.Parameters parameters) {
            Camera.Size result = null;
            camera.setDisplayOrientation(90);

            for (Camera.Size size : parameters.getSupportedPreviewSizes()) {
                if (size.width <= width && size.height <= height) {
                    if (result == null) {
                        result = size;
                    } else {
                        int resultArea = result.width * result.height;
                        int newArea = size.width * size.height;

                        if (newArea > resultArea) {
                            result = size;
                        }
                    }
                }
            }
            return (result);
        }

        //Initializing the Preview
        private void initPreview(int width, int height) {
            if (camera != null && previewHolder.getSurface() != null) {
                try {
                    camera.setPreviewDisplay(previewHolder);
                } catch (Throwable t) {
                    Log.e("PreviewDemo-surfaceCallback",
                            "Exception in setPreviewDisplay()", t);
                    //Toast.makeText(this, t.getMessage(),
                            //Toast.LENGTH_LONG).show();
                }

                if (!cameraConfigured) {
                    Camera.Parameters parameters = camera.getParameters();
                    Camera.Size size = getBestPreviewSize(width, height, parameters);

                    if (size != null) {
                        parameters.setPreviewSize(size.width, size.height);
                        camera.setParameters(parameters);
                        cameraConfigured = true;
                    }
                }
            }
        }

        //Starting the Preview
        private void startPreview() {
            if (cameraConfigured && camera != null) {
                camera.startPreview();
                inPreview = true;
            }
        }

        SurfaceHolder.Callback surfaceCallback = new SurfaceHolder.Callback() {
            public void surfaceCreated(SurfaceHolder holder) {
                // no-op -- wait until surfaceChanged()
            }

            public void surfaceChanged(SurfaceHolder holder, int format, int width,
                    int height) {
                initPreview(width, height);
                startPreview();
            }

            public void surfaceDestroyed(SurfaceHolder holder) {
                // no-op
            }
        };

However in my activity, when I try to use the camera.takepicture method I get a null pointer exception...

public class PhotoHandler extends FragmentActivity{

    private Camera camera;
    Button snap;
    ShutterCallback shutterCallback;
    PictureCallback rawCallback;
    PictureCallback jpegCallback;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.level_main_layout);

        buttonHandler();

    shutterCallback = new ShutterCallback() {
        public void onShutter() {
            Log.d("SHUTTER", "onShutter'd");
        }
    };

    /** Handles data for raw picture */
    rawCallback = new PictureCallback() {
        public void onPictureTaken(byte[] data, Camera camera) {
            Log.d("RAW", "onPictureTaken - raw");
        }
    };

    /** Handles data for jpeg picture */
    jpegCallback = new PictureCallback() {
        public void onPictureTaken(byte[] data, Camera camera) {
            FileOutputStream outStream = null;
            try {
                // write to local sandbox file system
                // outStream =
                // CameraDemo.this.openFileOutput(String.format("%d.jpg",
                // System.currentTimeMillis()), 0);
                // Or write to sdcard
                outStream = new FileOutputStream(String.format(
                        "/sdcard/%d.jpg", System.currentTimeMillis()));
                outStream.write(data);
                outStream.close();
                Log.d("JPG", "onPictureTaken - wrote bytes: " + data.length);
            } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                }
                Log.d("JPG", "onPictureTaken - jpeg");
            }
        };
    }


    public void buttonHandler() {

        snap = (Button) findViewById(R.id.takePic);

        snap.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                camera.takePicture(shutterCallback, rawCallback, jpegCallback);

            }

        });

    }
}

I am getting the null pointer exception when I click the button, so when it enters the takePicture() method.

here is my logcat..

07-17 16:45:58.906: E/AndroidRuntime(14603): FATAL EXCEPTION: main
07-17 16:45:58.906: E/AndroidRuntime(14603): java.lang.NullPointerException
07-17 16:45:58.906: E/AndroidRuntime(14603):    at com.purple.reepfragment.PhotoHandler$4.onClick(PhotoHandler.java:91)
07-17 16:45:58.906: E/AndroidRuntime(14603):    at android.view.View.performClick(View.java:4204)
07-17 16:45:58.906: E/AndroidRuntime(14603):    at android.view.View$PerformClick.run(View.java:17355)
07-17 16:45:58.906: E/AndroidRuntime(14603):    at android.os.Handler.handleCallback(Handler.java:725)
07-17 16:45:58.906: E/AndroidRuntime(14603):    at android.os.Handler.dispatchMessage(Handler.java:92)
07-17 16:45:58.906: E/AndroidRuntime(14603):    at android.os.Looper.loop(Looper.java:137)
07-17 16:45:58.906: E/AndroidRuntime(14603):    at android.app.ActivityThread.main(ActivityThread.java:5041)
07-17 16:45:58.906: E/AndroidRuntime(14603):    at java.lang.reflect.Method.invokeNative(Native Method)
07-17 16:45:58.906: E/AndroidRuntime(14603):    at java.lang.reflect.Method.invoke(Method.java:511)
07-17 16:45:58.906: E/AndroidRuntime(14603):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-17 16:45:58.906: E/AndroidRuntime(14603):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-17 16:45:58.906: E/AndroidRuntime(14603):    at dalvik.system.NativeStart.main(Native Method)

I'm just wondering if anyone could help me here, it's painful now after spending time on this and not able to move forward, so if anyone has any ideas they would be greatly appreciated..

2
Contributors
2
Replies
11
Views
4 Years
Discussion Span
Last Post by peter_budo
0

Apologies, it was a silly error on my behalf...I can't delete the post, so if admin are around they might?

0

Why not instead provide solution ;) (it doesn't matter how trivial can be someone may have run in similar situation in future)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.