从Stopped返回时,Android Simple Camera Control应用程序崩溃

时间:2015-03-02 14:18:37

标签: android camera android-lifecycle

我正在研究building a Camera App上的developer.Android教程,但我总是遇到以下问题:当我的应用程序被置于后台时,即根据活动生命周期停止,我尝试返回它它崩溃了,告诉我应用程序已停止工作。

我搜索了其他问题,但他们似乎都没有同样的问题,或者我无法识别它。

我试图找出发生此错误的最小代码量。 (仅限)活动:

public class CameraActivity extends ActionBarActivity {

private Camera mCamera;
private CameraPreview mPreview;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_camera_layout);

    // if device hardware has a camera
    if (checkCameraHardware(this))
        // Create an instance of Camera
        mCamera = getCameraInstance();

    // Create our Preview view and set it as the content of our activity.
    mPreview = new CameraPreview(this, mCamera);
    FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
    preview.addView(mPreview);


}

/** Check if this device has a camera */
private boolean checkCameraHardware(Context context) {
    if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
        // this device has a camera
        return true;
    } else {
        // no camera on this device
        return false;
    }
}

/** A safe way to get an instance of the Camera object. */
public static Camera getCameraInstance(){
    Camera c = null;
    try {
        c = Camera.open(); // attempt to get a Camera instance
    }
    catch (Exception e){
        // Camera is not available (in use or does not exist)
    }
    return c; // returns null if camera is unavailable
}

@Override
public void onPause() {
    super.onPause();
    releaseCamera();              // release the camera immediately on pause event
}

private void releaseCamera(){
    if (mCamera != null){
        mCamera.stopPreview();      // stop the preview
        mCamera.release();        // release the camera for other applications
        mCamera = null;
    }
}

@Override
public void onResume() {
    super.onResume();

    if (mCamera == null)
        mCamera = getCameraInstance();
}


}

使用此类实例化CameraPreview:

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
    private SurfaceHolder mHolder;
    private Camera mCamera;

public CameraPreview(Context context, Camera camera) {
    super(context);
    mCamera = camera;

    // Install a SurfaceHolder.Callback so we get notified when the
    // underlying surface is created and destroyed.
    mHolder = getHolder();
    mHolder.addCallback(this);
    // deprecated setting, but required on Android versions prior to 3.0
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

public void surfaceCreated(SurfaceHolder holder) {
    // The Surface has been created, now tell the camera where to draw the preview.
    try {

         mCamera.setPreviewDisplay(holder);
         mCamera.startPreview();

    } catch (IOException e) {
        Log.d("PandaHero", "Error setting camera preview: " + e.getMessage());
    }
}

public void surfaceDestroyed(SurfaceHolder holder) {
    // empty. Take care of releasing the Camera preview in your activity.
}

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
    // .. same as in the example of the link
}
}

对于错误消息,它总是告诉我在release()之后调用surfaceCreated()。这让我觉得我必须错过一些重要的相机重新实现或预览某处。我试图停止预览并在surfaceDestroyed(),onStop()中释放相机,并重新打开onStart()内部的预览,但错误仍然存​​在。 此外,如果我不首先包含预览,则不会发生崩溃(但我的用户也无法看到它,所以这是没有选择的。)

所以是的,我认为在利用生命周期方面,我在这里缺少一些重要的东西。

编辑:我提到的错误消息。

03-02 14:33:45.553  15462-15462/com.example.vrevru.viewlate_camerasetupsimple E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Method called after release()
        at android.hardware.Camera.setPreviewDisplay(Native Method)
        at android.hardware.Camera.setPreviewDisplay(Camera.java:428)
        at com.example.thorsten.viewlate_camerasetupsimple.CameraPreview.surfaceCreated(CameraPreview.java:32)
        at android.view.SurfaceView.updateWindow(SurfaceView.java:639)
        at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:262)
        at android.view.View.dispatchWindowVisibilityChanged(View.java:7440)
        at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1042)
        at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1042)
        at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1042)
        at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1042)
        at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1042)
        at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1042)
        at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1042)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1237)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1009)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4236)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
        at android.view.Choreographer.doCallbacks(Choreographer.java:555)
        at android.view.Choreographer.doFrame(Choreographer.java:525)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
        at android.os.Handler.handleCallback(Handler.java:615)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4872)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
        at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

好的,

我认为我有一个解决方案,但我不确定它是最好的解决方案。

我注意到在onPause()中释放旧的后,我从未告诉我的应用创建新的预览。我认为Android会处理这个问题,因为文档总是说没有必要重做第一次创建应用程序的设置。显然,我误解了他们(虽然他们的意思是什么?)。

所以我在onPause()中添加了一个从Framelayout手动删除旧CameraPreview对象,然后创建了一个新对象并将其添加到onResume()中的Framelayout。

@Override
public void onPause() {
    super.onPause();
    releaseCamera();              // release the camera immediately on pause event
    // two new lines for removing the old CameraPreview
    FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
    preview.removeView(mPreview);
}
@Override
public void onResume() {
    super.onResume();

    if (mCamera == null)
    {
        mCamera = getCameraInstance();
        // three new lines, creating a new CameraPreview, then adding it to the FrameLayout
        mPreview = new CameraPreview(this, mCamera);
        FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
        preview.addView(mPreview);
    }
}

然而,虽然现在错误已经消失并且我的问题已经解决(应用程序不再崩溃),我只是将一些看似相当昂贵的代码行放入我的Pause和Resume函数中。向其他应用程序和后台的过渡已经显着减慢。有更聪明的方法吗?