title: Camera.open() throwing java.lang.RuntimeException: Fail to connect to camera service
body: <p>I am using custom camera in my application.for the first time i have loaded the app it was working.but after that it is crashing at the point Camera.open();.i have given all the permissions in the manifest file.given below is my Activity class</p>
  
  <pre><code>public class CustomCameraDemo extends Activity {
  
      private SurfaceView preview=null;   
      private SurfaceHolder previewHolder=null;   
      public Camera camera=null;    
      private boolean inPreview=false;   
      //ImageView image;   
      Bitmap bmp,itembmp;  
      static Bitmap mutableBitmap;   
      PointF start = new PointF();   
      PointF mid = new PointF();   
      float oldDist = 1f;   
      File imageFileName = null;   
      File imageFileFolder = null;   
      private MediaScannerConnection msConn;  
      Display d;   int screenhgt,screenwdh;       
      ProgressDialog dialog;
      Button save;
      Button retake;
      ImageView capture;
  
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
         // image=(ImageView)findViewById(R.id.image);  
  
          capture=(ImageView)findViewById(R.id.capture);
          save=(Button)findViewById(R.id.Save);
          retake=(Button)findViewById(R.id.Retake);
  
  
          preview=(SurfaceView)findViewById(R.id.surface);      
          previewHolder=preview.getHolder();     
          previewHolder.addCallback(surfaceCallback);     
          previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);      
          previewHolder.setFixedSize(getWindow().getWindowManager()     
                  .getDefaultDisplay().getWidth(), getWindow().getWindowManager()     
                  .getDefaultDisplay().getHeight()); 
  
          save.setOnClickListener(new OnClickListener() {
  
              @Override
              public void onClick(View v) {
  
  
  
              }
          });
          retake.setOnClickListener(new OnClickListener() {
  
              @Override
              public void onClick(View v) {
  
                  camera.stopPreview();
              }
          });
          capture.setOnClickListener(new OnClickListener() {
  
              @Override
              public void onClick(View v) {
                  onBack(); 
  
              }
          });
  
      }
  
  
      @Override   
      public void onResume() {  
          super.onResume(); 
          camera=Camera.open();   
          }   
      @Override  
      public void onPause() {   
              if (inPreview) {   
                  camera.stopPreview();  
                  }    
           camera.release();   
           camera=null;   
            inPreview=false;   
      super.onPause();  
      } 
  
      private Camera.Size getBestPreviewSize(int width, int height,Camera.Parameters parameters){   
          Camera.Size result=null;   
          for (Camera.Size size : parameters.getSupportedPreviewSizes()) 
          {   
              if (size.width&lt;=width &amp;&amp; size.height&lt;=height)
              {
                  if (result==null) {  
                      result=size;   
                  }   else {  
                      int resultArea=result.width*result.height; 
                      int newArea=size.width*size.height;  
                      if (newArea&gt;resultArea) {   
                          result=size;   
                          }  
                      }   
                  }   
              }   
          return(result);   
          }    
          SurfaceHolder.Callback surfaceCallback=new SurfaceHolder.Callback(){
  
          public void surfaceCreated(SurfaceHolder holder) {     
              try {        
                  camera.setPreviewDisplay(previewHolder); 
                  }   catch (Throwable t) {   
                      Log.e("PreviewDemo-surfaceCallback",
                              "Exception in setPreviewDisplay()", t);
                      Toast.makeText(CustomCameraDemo.this, t.getMessage(), Toast.LENGTH_LONG).show();       
                      }     
                  }      
          public void surfaceChanged(SurfaceHolder holder,int format, int width,int height) {
              Camera.Parameters parameters=camera.getParameters();       
              Camera.Size size=getBestPreviewSize(width, height,                                           
                      parameters);       
              if (size!=null) {      
                  parameters.setPreviewSize(size.width, size.height);
                  camera.setParameters(parameters);       
                  camera.startPreview();       
                  //inPreview=true;       
                  }     
              }      
          public void surfaceDestroyed(SurfaceHolder holder) {
  
              }   
          };         
  
          Camera.PictureCallback photoCallback=new Camera.PictureCallback(){       
  
              public void onPictureTaken(final byte[] data, final Camera camera){      
                          dialog=ProgressDialog.show(CustomCameraDemo.this,"","Saving Photo");       
                          new Thread(){       
                              public void run(){       
                                  try{      
                                      Thread.sleep(1000);
                                      }       
                                  catch(Exception ex){}       
                                  onPictureTake(data,camera);
                                  }       
                              }.start();             
                              }       
                      };    
                      public void onPictureTake(byte[] data, Camera camera){        
                          bmp = BitmapFactory.decodeByteArray(data, 0, data.length);       
                          mutableBitmap = bmp.copy(Bitmap.Config.ARGB_8888, true);       
                          savePhoto(mutableBitmap);       
                          dialog.dismiss();       
                          }     
                      class SavePhotoTask extends AsyncTask&lt;byte[], String, String&gt; {
                          @Override       
                          protected String doInBackground(byte[]... jpeg) {
                              File photo=new File(Environment.getExternalStorageDirectory(),"photo.jpg");
                              if (photo.exists()){
                                  photo.delete();
                                  }       try {      
                                      FileOutputStream fos=new FileOutputStream(photo.getPath()); 
                                      fos.write(jpeg[0]);       
                                      fos.close();       
                                      } catch (java.io.IOException e) {
                                          Log.e("PictureDemo", "Exception in photoCallback", e);
                                          }      
                                      return(null);
                                          }       
                          }  
              public void savePhoto(Bitmap bmp) {
                          imageFileFolder = new File(Environment.getExternalStorageDirectory(),"MyMedicalRecords");
                          imageFileFolder.mkdir();
                          FileOutputStream out = null; 
                          Calendar c = Calendar.getInstance();
                          String date = fromInt(c.get(Calendar.MONTH))+ fromInt(c.get(Calendar.DAY_OF_MONTH))
                          + fromInt(c.get(Calendar.YEAR))             
                          + fromInt(c.get(Calendar.HOUR_OF_DAY))            
                          + fromInt(c.get(Calendar.MINUTE))           
                          + fromInt(c.get(Calendar.SECOND)); 
                          imageFileName = new File(imageFileFolder, date.toString() + ".jpg");
                          try {  out = new FileOutputStream(imageFileName);  
                          bmp.compress(Bitmap.CompressFormat.JPEG, 100, out);  
                          out.flush();  out.close();  
                          scanPhoto(imageFileName.toString());  
                          out = null; 
                          } catch (Exception e) { e.printStackTrace(); } 
                          }  
              public String fromInt(int val) {
                              return String.valueOf(val);
                              } 
              public void scanPhoto(final String imageFileName) {
                              msConn = new MediaScannerConnection(CustomCameraDemo.this,new MediaScannerConnectionClient() {
              public void onMediaScannerConnected() {
                                      msConn.scanFile(imageFileName, null); 
                                      Log.i("msClient obj  in Photo Utility","connection established"); 
                                      } 
              public void onScanCompleted(String path, Uri uri) {
                                          msConn.disconnect(); Log.i("msClient obj in Photo Utility","scan completed"); 
                                          }
                                      }); 
                              msConn.connect(); 
                              } 
  
  
              public void onBack(){ Log.e("onBack :","yes"); 
                          camera.takePicture(null,null,photoCallback); 
                          //inPreview=false; 
              } 
  
      }
  </code></pre>