Search code examples
androidcamerastartuponcreate

Start camera on app startup


I want to build an app that immediately runs the camera, taking pictures and after that do things with the pictures. I use this two methods to take a picture and save it to the device:

static final int REQUEST_TAKE_PHOTO = 1;

private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    // Ensure that there's a camera activity to handle the intent
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        // Create the File where the photo should go
        File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
            // Error occurred while creating the File
            Toast.makeText(this, "Failed to save to picture", Toast.LENGTH_LONG).show();
        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                    Uri.fromFile(photoFile));
            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
        }
    }
}
String mCurrentPhotoPath;

private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );

    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = "file:" + image.getAbsolutePath();
    return image;
}

This is my onCreate method:

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_camera);
}

I want to start the camera immediately on the start of the app. How can i do it?


Solution

  • Do not put your method in the onCreate: it might not be called if the activity is never destroyed (and it happens quite frequently, indeed). This will do the trick.

    @Override
    protected void onResume() {
        super.onResume();
    
        dispatchTakePictureIntent();
    }
    

    UPDATE: since nothing happens, I suggest you these steps to debug the problem:

    1. Log the call of the method, in order to understand whether it's called or not:

      private void dispatchTakePictureIntent() {
          Log.d("CameraApp", "dispatchTakePictureIntent was called");
          // ...
      }
      
    2. Log the possible issues, without ignoring the "elses" clauses:

      private void dispatchTakePictureIntent() {
          // ...
          if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
              // ...
              if (photoFile != null) {
                  // ...
              } else {
                  Log.e("CameraApp", "photoFile was null");
              }
          } else {
              Log.e("CameraApp", "No activity available to call the intent");
          }
      }
      

    In this way you can at least detect the reason why nothing happens:

    1. In case you never see "dispatchTakePictureIntent was called", there's a problem in the onResume method, since dispatchTakePictureIntent is actually never called

    2. In case the other two logs are printed in the Logcat, then you have a problem with the intents and the code in your method.