title: Android - Camera Intent Causes the App to Crash
body: <p>I'm trying to activate camera with intent by the following codes :</p>
  
  <pre><code>import android.content.Intent;
  import android.graphics.Bitmap;
  import android.graphics.BitmapFactory;
  import android.net.Uri;
  import android.os.Bundle;
  import android.os.Environment;
  import android.provider.MediaStore;
  import android.support.v7.app.AppCompatActivity;
  import android.util.Log;
  import android.view.View;
  import android.widget.Button;
  import android.widget.ImageView;
  import android.widget.Toast;
  import java.io.File;
  import java.io.IOException;
  import java.text.SimpleDateFormat;
  import java.util.Date;
  
  public class MainActivity extends AppCompatActivity {
      private static final String TAG = "MainActivity";
      private ImageView mImageView;
      static final int REQUEST_IMAGE_CAPTURE = 1;
      static final int REQUEST_TAKE_PHOTO = 1;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          Button button = (Button) findViewById(R.id.button);
          mImageView = findViewById(R.id.imageView);
  
          button.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  Toast.makeText(MainActivity.this, "clicked", Toast.LENGTH_SHORT).show();
                  dispatchTakePictureIntent();
              }
          });
      }
  
      @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
          Log.d(TAG, "onActivityResult: 0");
          if (requestCode == REQUEST_IMAGE_CAPTURE &amp;&amp; resultCode == RESULT_OK) {
              Log.d(TAG, "onActivityResult: 1");
  
              Bundle extras = data.getExtras();
              Bitmap imageBitmap = (Bitmap) extras.get("data");
              mImageView.setImageBitmap(imageBitmap);
          }
      }
  
      private void dispatchTakePictureIntent() {
          Log.d(TAG, "dispatchTakePictureIntent: 0");
          Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
          if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
              Log.d(TAG, "dispatchTakePictureIntent: 1");
              startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
          }
      }
  }
  </code></pre>
  
  <p>The app crashes every time I click the button(it's supposed to open the camera). I can get "dispatchTakePictureIntent: 1" from logcat, so takePictureIntent.resolveActivity(getPackageManager()) isn't returning null. I think it's startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE) that causes the app to crash.</p>
  
  <p>The codes are replicated from the official documents : <a href="https://developer.android.com/training/camera/photobasics#java" rel="nofollow noreferrer">https://developer.android.com/training/camera/photobasics#java</a>.</p>
  
  <p>I've tried dispatchTakePictureIntent(View v), but the app still crashed. I've also replaced dispatchTakePictureIntent() with the method below and it still crashed.</p>
  
  <pre><code>private void TakePicture(){
      Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
      startActivityForResult(intent, 0);
  }
  </code></pre>
  
  <p>I've been trying other solutions I found on the internet, non of them helped. What could be the problem that causes my app to crash?</p>