如何处理相机运行时异常?

时间:2011-05-13 09:00:02

标签: android

我的应用程序工作完美但当我的相机应用时启动和关闭然后再次启动然后。 java.lang.RuntimeException:无法连接到摄像头服务 这个错误生成Plz任何身体帮助我。

感谢您前进......

代码 `   的 CameraActivity.java

 package com.drc.camera;

  public class CameraActivity extends Activity { 

     private static final String TAG = "CameraDemo";
      CameraPreview mCameraPreview=null;
      FrameLayout mFrameLayout;
      Button btntakephoto,btnresetphoto; 

      @Override
      public void onCreate(Bundle savedInstanceState)       
      {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.xcamera);
        mFrameLayout=(FrameLayout)findViewById(R.id.framepreview);
            btntakephoto = (Button) findViewById(R.id.btntakephoto);
        btnresetphoto = (Button) findViewById(R.id.btnresetphoto);


        mCameraPreview = new CameraPreview(this); 
        mFrameLayout.addView(mCameraPreview);

        btnresetphoto.setEnabled(false);

        btntakephoto.setOnClickListener(new OnClickListener() {
          public void onClick(View v) { 
            mCameraPreview.camera.takePicture(shutterCallback, rawCallback, jpegCallback);
            btnresetphoto.setEnabled(true);
          }
        });


        btnresetphoto.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                mCameraPreview.camera.startPreview();
                btnresetphoto.setEnabled(false);
                Toast.makeText(CameraActivity.this,"Start Camera", Toast.LENGTH_SHORT).show();
            }
        });

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

      PictureCallback rawCallback = new PictureCallback() {
        public void onPictureTaken(byte[] data, Camera camera) {
          Log.d(TAG, "onPictureTaken - raw");
        }
      };

      PictureCallback jpegCallback = new PictureCallback() { 
        public void onPictureTaken(byte[] data, Camera camera) {
          FileOutputStream outStream = null;
          try {
                outStream = new FileOutputStream(String.format("/sdcard/%d.jpg",System.currentTimeMillis()));
                outStream.write(data);
                outStream.close();
                Log.d(TAG, "onPictureTaken - wrote bytes: " + data.length);
              }
          catch (FileNotFoundException e) {
                e.printStackTrace();
              }
          catch (IOException e) {
                e.printStackTrace();
              }
          Log.d(TAG, "onPictureTaken - jpeg");
        }
      };

}

`
`

CameraPreview.java

 package com.drc.camera;

    import java.io.IOException;

    class CameraPreview extends SurfaceView implements SurfaceHolder.Callback { 
      private static final String TAG = "Preview";

      SurfaceHolder mHolder;  
      public Camera camera; 

      CameraPreview(Context context) {
        super(context);
        mHolder = getHolder();  
        mHolder.addCallback(this);  
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
      }

      public void surfaceCreated(SurfaceHolder holder) {  

        camera = Camera.open(); 
        try {
          camera.setPreviewDisplay(holder); 

          camera.setPreviewCallback(new PreviewCallback() { 
            public void onPreviewFrame(byte[] data, Camera camera) {  
              Log.d(TAG, "onPreviewFrame called at: " + System.currentTimeMillis());
              CameraPreview.this.invalidate(); 
            }
          });
        }
       catch (IOException e) { 
          e.printStackTrace();
        }
      }

      public void surfaceDestroyed(SurfaceHolder holder) {  
        camera.stopPreview();
        camera = null;
      }

      public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
            Camera.Parameters parameters = camera.getParameters();
            parameters.setPreviewSize(w, h);
            camera.setParameters(parameters);
            camera.startPreview();
        }

        @Override
        public void draw(Canvas canvas) {
            super.draw(canvas);
            Paint p = new Paint(Color.RED);
            Log.d(TAG, "draw");
            canvas.drawText("PREVIEW", canvas.getWidth() / 2,canvas.getHeight() / 2, p);
        }

    }

` 的的AndroidManifest.xml

    

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".CameraActivity"
              android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
              android:screenOrientation="landscape"
              android:configChanges="orientation|keyboardHidden"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera"></uses-feature> 

1 个答案:

答案 0 :(得分:2)

确保在onPause()中调用camera.release(),而不是将其设置为null。允许应用程序在不在前台时保持摄像机运行(例如,这样您可以在检查不同应用程序中的某些内容时继续使用视频聊天)。

这意味着一旦完成相机操作后必须自行清理,并调用release()。在您这样做之前,或者至少在GC清理或您的申请流程被杀之前,其他任何应用都无法访问相机。