Question

I have this code for my app, it doesn't show any errors but everytime I run it on my phone, it crashes. Anybody have idea about this? Thanks

public class CameraView extends Activity implements SurfaceHolder.Callback,OnClickListener {
        static final int FOTO_MODE = 0;
        private static final String TAG = "CameraTest";
        Camera mCamera;
        boolean mPreviewRunning = false;
        private Context mContext = this;

        public void onCreate(Bundle icicle) {
            super.onCreate(icicle);

            Log.e(TAG, "onCreate");

            Bundle extras = getIntent().getExtras();

            getWindow().setFormat(PixelFormat.TRANSLUCENT);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
            setContentView(R.layout.camera_surface);
            mSurfaceView = (SurfaceView) findViewById(R.id.surface_camera);
            mSurfaceView.setOnClickListener(this);
            mSurfaceHolder = mSurfaceView.getHolder();
            mSurfaceHolder.addCallback(this);
            mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        }
        //private SurfaceView mSurfaceView;
        //private SurfaceHolder mSurfaceHolder;

        @Override
        protected void onRestoreInstanceState(Bundle savedInstanceState) {
            super.onRestoreInstanceState(savedInstanceState);
        }

        Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
            public void onPictureTaken(byte[] imageData, Camera c) {

                if (imageData != null) {

                    Intent mIntent = new Intent();

                    StoreByteImage(mContext, imageData, 50,
                            "ImageName");
                    mCamera.startPreview();

                    setResult(FOTO_MODE, mIntent);
                    finish();

                }
            }
        };

        protected void onResume() {
            Log.e(TAG, "onResume");
            super.onResume();
        }

        protected void onSaveInstanceState(Bundle outState) {
            super.onSaveInstanceState(outState);
        }

        protected void onStop() {
            Log.e(TAG, "onStop");
            super.onStop();
        }

        public void surfaceCreated(SurfaceHolder holder) {
            Log.e(TAG, "surfaceCreated");
            mCamera = Camera.open();

        }

        private Camera.Size getBestPreviewSize(int width, int height)
        {
            Camera.Size result=null;    
            Camera.Parameters p = mCamera.getParameters();
            for (Camera.Size size : p.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;
        }

        public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
            Log.e(TAG, "surfaceChanged");

            // XXX stopPreview() will crash if preview is not running
            if (mPreviewRunning) {
                mCamera.stopPreview();
            }

            Camera.Parameters p = mCamera.getParameters();
            Camera.Size bestSize = getBestPreviewSize(w, h);
            p.setPreviewSize(bestSize.width, bestSize.height);
            mCamera.setParameters(p);
            try {
                mCamera.setPreviewDisplay(holder);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            mCamera.startPreview();
            mPreviewRunning = true;
        }

        public void surfaceDestroyed(SurfaceHolder holder) {
            Log.e(TAG, "surfaceDestroyed");
            mCamera.stopPreview();
            mPreviewRunning = false;
            mCamera.release();
        }

        private SurfaceView mSurfaceView;
        private SurfaceHolder mSurfaceHolder;

        public void onClick(View arg0) {

            mCamera.takePicture(null, mPictureCallback, mPictureCallback);

        }

        public static boolean StoreByteImage(Context mContext, byte[] imageData,
                int quality, String expName) {

            File sdImageMainDirectory = new File("/sdcard");
            FileOutputStream fileOutputStream = null;
            String nameFile;
            try {

                BitmapFactory.Options options=new BitmapFactory.Options();
                options.inSampleSize = 5;

                Bitmap myImage = BitmapFactory.decodeByteArray(imageData, 0,
                        imageData.length,options);


                fileOutputStream = new FileOutputStream(
                        sdImageMainDirectory.toString() +"/image.jpg");


                BufferedOutputStream bos = new BufferedOutputStream(
                        fileOutputStream);

                myImage.compress(CompressFormat.JPEG, quality, bos);

                bos.flush();
                bos.close();

            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return true;
        }

        /*@Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
            // TODO Auto-generated method stub
            Log.e(TAG, "surfaceChanged");

            // XXX stopPreview() will crash if preview is not running
            if (mPreviewRunning) {
                mCamera.stopPreview();
            }

            Camera.Parameters p = mCamera.getParameters();
            p.setPreviewSize(w, h);
            mCamera.setParameters(p);
            try {
                mCamera.setPreviewDisplay(holder);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            mCamera.startPreview();
            mPreviewRunning = true;
        }

        @Override
        public void surfaceCreated(SurfaceHolder arg0) {
            // TODO Auto-generated method stub
            Log.e(TAG, "surfaceCreated");
            mCamera = Camera.open();

        }

        @Override
        public void surfaceDestroyed(SurfaceHolder arg0) {
            // TODO Auto-generated method stub
            Log.e(TAG, "surfaceDestroyed");
            mCamera.stopPreview();
            mPreviewRunning = false;
            mCamera.release();

        }*/
}
Was it helpful?

Solution

Do you know where the error is actually occurring (i.e. what output are you seeing in the logcat window?)

My wild guess is that it's this line causing the problem:

p.setPreviewSize(w, h)

You can only set the preview size to one of the sizes that your device supports - check the getSupportedPreviewSizes() method in Camera.Parameters and choose the closest match for your view:

private Camera.Size getBestPreviewSize(int width, int height)
{
    Camera.Size result=null;    
    Camera.Parameters p = camera.getParameters();
    for (Camera.Size size : p.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;
}

(code above from @seantron: Picture distorted with Camera and getOptimalPreviewSize)

OTHER TIPS

surround setPreview() of camera with try & catch Block, may this work

I had a similar issue, and the cause was the implementation of the surfaceDestroyed(SurfaceHolder holder) method. When I commented out the code, all was well.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top