-1

i have an android application which has two camera activities. they switch time to time in between them. after switching several times first activity throws this exception.. any idea why this happen? how to fix it? Please help me.thank you for reading my question and have a nice day!

enter image description here

     public abstract class SampleViewBase extends SurfaceView implements SurfaceHolder.Callback, Runnable {
private static final String TAG = "LogcatActivity";

private Camera              mCamera;
private SurfaceHolder       mHolder;
private int                 mFrameWidth;
private int                 mFrameHeight;
private byte[]              mFrame;
private boolean             mThreadRun;
private int                 frameNumber=1;//my


public SampleViewBase(Context context) {///
    super(context);
    mHolder = getHolder();
    mHolder.addCallback(this);
    Log.i(TAG, "Instantiated new " + this.getClass());

}




public int getFrameWidth() {
    return mFrameWidth;
}

public int getFrameHeight() {
    return mFrameHeight;
}

public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
    Log.i(TAG, "surfaceCreated");
    if (mCamera != null) {
        Camera.Parameters params = mCamera.getParameters();
        List<Camera.Size> sizes = params.getSupportedPreviewSizes();
        //--
        List<String> flashing_methords=params.getSupportedFlashModes();
        params.setFlashMode(flashing_methords.get(3));

       // List<String> color_effects=params.getSupportedColorEffects();
       // params.setColorEffect(color_effects.get(2));
        //--
        mFrameWidth = width;
        mFrameHeight = height;

        // selecting optimal camera preview size
        {
            double minDiff = Double.MAX_VALUE;
            for (Camera.Size size : sizes) {
                if (Math.abs(size.height - height) < minDiff) {
                    mFrameWidth = size.width;
                    mFrameHeight = size.height;
                    minDiff = Math.abs(size.height - height);
                }
            }
        }

        params.setPreviewSize(getFrameWidth(), getFrameHeight());
        mCamera.setParameters(params);
        mCamera.startPreview();
    }
}

public void surfaceCreated(SurfaceHolder holder) {
    Log.i(TAG, "surfaceCreated");
    try{
    mCamera.reconnect();
    mCamera = Camera.open();
    mCamera.setPreviewCallback(new PreviewCallback() {
        public void onPreviewFrame(byte[] data, Camera camera) {
            synchronized (SampleViewBase.this) {
                mFrame = data;
                SampleViewBase.this.notify();
            }
            /*  if((frameNumber%120)==0){

                    synchronized (SampleViewBase.this) {
                        mFrame = data;
                        SampleViewBase.this.notify();
                        frameNumber=1;
                }

            }else{
                frameNumber++;
            }*/
        }
    });
    (new Thread(this)).start();
    }catch (Exception e) {
        Log.v(TAG, "reconnect error" + e);
    }

}

public void surfaceDestroyed(SurfaceHolder holder) {
    Log.i(TAG, "surfaceDestroyed");
    mThreadRun = false;
    if (mCamera != null) {
        synchronized (this) {
            mCamera.stopPreview();
            mCamera.setPreviewCallback(null);

            mCamera.release();
            mCamera = null;
        }
    }
}

protected abstract Bitmap processFrame(byte[] data);

public void run() {
    mThreadRun = true;
    Log.i(TAG, "Starting processing thread");
    while (mThreadRun) {
        Bitmap bmp = null;

        synchronized (this) {
            try {
                this.wait();
                bmp = processFrame(mFrame);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        if (bmp != null) {
            Canvas canvas = mHolder.lockCanvas();
            if (canvas != null) {
                canvas.drawBitmap(bmp, (canvas.getWidth() - getFrameWidth()) / 2, (canvas.getHeight() - getFrameHeight()) / 2, null);
                mHolder.unlockCanvasAndPost(canvas);
            }
            bmp.recycle();
        }
    }
}



public void releaseAll() {
    Log.i(TAG, "hardweare released");
    mThreadRun = false;
    if (mCamera != null) {
        synchronized (this) {
            mCamera.stopPreview();
            mCamera.setPreviewCallback(null);

            mCamera.release();
            mCamera = null;
        }
    }
}

}

user1476945
  • 113
  • 1
  • 8
  • Take a look at this: http://developer.android.com/reference/android/hardware/Camera.html#setOneShotPreviewCallback – TheBlueCat Jul 15 '12 at 11:04
  • as i told u previously it workes some ammount of time.after that it struks(force close!).could n't find any thing important in the given link :( – user1476945 Jul 15 '12 at 13:32

2 Answers2

2

Try to release your camera resources from your activity after you override the back key.

@Override
public void onBackPressed()
{
    releaseCamera();
    super.onBackPressed();
}

private void releaseCamera()
{
    synchronized (this)
    {
        if(mCamera!=null)
        {
            mCamera.stopPreview();
            mCamera.setPreviewCallback(null);
            mCamera.release();
            mCamera = null;
        }
    }
}

add the method releaseCamera() to your SurfaceView and make the call from your activity when user clicks back button.

moh.sukhni
  • 2,220
  • 19
  • 24
0

the mCamera.release() function means to disconnect and release the Camera object resources. which performs a job identical to your next mCamera = null; statement. Why would you do that?

Vinay W
  • 9,912
  • 8
  • 41
  • 47
  • if u take a look at android camera example (in sdk) they have done the same thing. and again im sure `mCamera = null;` and mCamera.release() are not equal! – user1476945 Jul 15 '12 at 13:37