java.lang.RuntimeException:release()之后的方法调用

时间:2012-07-15 11:00:36

标签: android thread-safety android-camera runtimeexception

我有一个Android应用程序,它有两个相机活动。他们在他们之间不时切换。切换几次后,第一次活动抛出此异常..任何想法为什么会发生这种情况? 怎么解决?请帮帮我。谢谢你阅读我的问题,祝你有愉快的一天!

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;
        }
    }
}

}

2 个答案:

答案 0 :(得分:2)

尝试在覆盖后退键后从活动中释放相机资源。

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

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

将方法releaseCamera()添加到SurfaceView,并在用户点击后退按钮时从您的活动中调用。

答案 1 :(得分:0)

mCamera.release()函数意味着断开连接并释放Camera对象资源。执行与下一个mCamera = null;语句相同的工作。你为什么这样做?