title: Android: Camera.Open() returns null with one Project but Not the other. Same Device
body: <p>I am learning the Android Camera Api and i am testing it on a Nexus 6 device. I downloaded a Camera Api tutorial which runs fine on the device. I then created my own project and essentially copied the code but that one always returns null whenever i call Camera.open(). The code is virtually identical. </p>
  
  <p>The Manifests are also identical and so are my gradle files.</p>
  
  <p>But the tutorial i downloaded from the website works while the one i created from Android Studio does not.</p>
  
  <p>I cant figure out what i am doing wrong.I have checked the previous questions regarding this issue and i am not making the same mistake. The Manifest permissions are in the proper location (above the application tag), the Nexus 6 has a back camera so no camera is not an issue. </p>
  
  <p><strong>Non-Working Manifest</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.gr3ymatter.cameraapidemo"&gt;
      &lt;uses-permission android:name="android.permission.CAMERA" /&gt;
      &lt;uses-feature android:name="android.hardware.camera" /&gt;
      &lt;uses-feature android:name="android.hardware.camera.autofocus" /&gt;
      &lt;application
          android:allowBackup="true"
          android:icon="@mipmap/ic_launcher"
          android:label="@string/app_name"
          android:theme="@style/AppTheme" &gt;
          &lt;activity
              android:name=".MainActivity"
              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;/manifest&gt;
  </code></pre>
  
  <p><strong>Non-Working MainActivity:</strong></p>
  
  <pre><code>import android.content.pm.ActivityInfo;
  import android.graphics.Bitmap;
  import android.graphics.BitmapFactory;
  import android.hardware.Camera;
  import android.os.Bundle;
  import android.support.v7.app.ActionBarActivity;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  import android.widget.Button;
  import android.widget.FrameLayout;
  import android.widget.ImageView;
  import android.widget.Toast;
  
  public class MainActivity extends ActionBarActivity {
  
      private ImageSurfaceView mImageSurfaceView;
      private Camera camera;
  
      private FrameLayout cameraPreviewLayout;
      private ImageView capturedImageHolder;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  
          cameraPreviewLayout = (FrameLayout)findViewById(R.id.camera_preview);
          capturedImageHolder = (ImageView)findViewById(R.id.captured_image);
  
          camera = checkDeviceCamera();
          mImageSurfaceView = new ImageSurfaceView(MainActivity.this, camera);
          cameraPreviewLayout.addView(mImageSurfaceView);
  
          Button captureButton = (Button)findViewById(R.id.button);
          captureButton.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  camera.takePicture(null, null, pictureCallback);
              }
          });
      }
      private Camera checkDeviceCamera(){
          Camera mCamera = null;
          try {
              mCamera = Camera.open();
          } catch (Exception e) {
              e.printStackTrace();
          }
          return mCamera;
      }
  
      Camera.PictureCallback pictureCallback = new Camera.PictureCallback() {
          @Override
          public void onPictureTaken(byte[] data, Camera camera) {
              Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
              if(bitmap==null){
                  Toast.makeText(MainActivity.this, "Captured image is empty", Toast.LENGTH_LONG).show();
                  return;
              }
              capturedImageHolder.setImageBitmap(scaleDownBitmapImage(bitmap, 300, 200 ));
          }
      };
  
      private Bitmap scaleDownBitmapImage(Bitmap bitmap, int newWidth, int newHeight){
          Bitmap resizedBitmap = Bitmap.createScaledBitmap(bitmap, newWidth, newHeight, true);
          return resizedBitmap;
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.menu_main, menu);
          return true;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          // Handle action bar item clicks here. The action bar will
          // automatically handle clicks on the Home/Up button, so long
          // as you specify a parent activity in AndroidManifest.xml.
          int id = item.getItemId();
  
          //noinspection SimplifiableIfStatement
          if (id == R.id.action_settings) {
              return true;
          }
  
          return super.onOptionsItemSelected(item);
      }
  }
  </code></pre>
  
  <p><strong>Non-Working SurfaceView Class</strong></p>
  
  <pre><code>import android.content.Context;
  import android.hardware.Camera;
  import android.view.SurfaceHolder;
  import android.view.SurfaceView;
  
  import java.io.IOException;
  
  /**
   * Created by Afzal on 2/2/16.
   */
  
  public class ImageSurfaceView extends SurfaceView implements SurfaceHolder.Callback {
  
      private Camera camera;
      private SurfaceHolder surfaceHolder;
  
      public ImageSurfaceView(Context context, Camera camera) {
          super(context);
          this.camera = camera;
          this.surfaceHolder = getHolder();
          this.surfaceHolder.addCallback(this);
      }
  
      @Override
      public void surfaceCreated(SurfaceHolder holder) {
          try {
              this.camera.setPreviewDisplay(holder);
              this.camera.startPreview();
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  
      @Override
      public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
  
      }
  
      @Override
      public void surfaceDestroyed(SurfaceHolder holder) {
          this.camera.stopPreview();
          this.camera.release();
      }
  }
  </code></pre>
  
  <p>The Working Projects files are the same as i literally copied them while i was following the example. I can paste a github link to both working and nonworking projects if that would help?</p>
  
  <p>EDIT: Here are repos for the two projects
  <a href="https://github.com/Gr3ymatter/CameraApiDemo_Working" rel="nofollow noreferrer">https://github.com/Gr3ymatter/CameraApiDemo_Working</a>
  <a href="https://github.com/Gr3ymatter/CameraApiDemo_NotWorking" rel="nofollow noreferrer">https://github.com/Gr3ymatter/CameraApiDemo_NotWorking</a></p>
  
  <p>EDIT: LOGCAT output. Also based on Madhurs helpful comments i thought maybe this is a device issue, so i ran this on the Nexus 7 but i still got the same issue.</p>
  
  <p><a href="https://i.stack.imgur.com/wLSRj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wLSRj.png" alt="enter image description here"></a></p>
  
  <p>Thank you in advance for the help!</p>