How To Handle Camera Runtime Exception?
<p>My Application Work Perfect But When My Camera Appliaction When Start And Close  Then Again Start Then.
java.lang.RuntimeException: Fail to connect to camera service
This Error Generate Plz Any Body Help Me.</p>

<p>Thanks For Advance...</p>

<p>Code
`
  <strong>CameraActivity.java</strong></p>

<pre><code> 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");
        }
      };

}

`
`
</code></pre>

<p><strong>CameraPreview.java</strong></p>

<pre><code> 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);
        }

    }
</code></pre>

<p>`
<strong>AndroidManifest.xml</strong></p>

<p>

    </p>

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

<p></p>
