Take a picture using the default camera

Add a file file_paths.xml to the resources directory res/xml/ in order to grant access to the correct folders:

	<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path
        name="external_files"
        path="."/>
</paths>

In your AndroidManifest.xml add the following:

<manifest ... >
  
    <uses-feature android:name="android.hardware.camera"
                  android:required="true" />
    ...
    <application>
        ...
        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.pixplicity.example.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>
    </application>
</manifest>

Then launch the camera like so:

private void launchCamera() {
  Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  // Check which app can handle taking pictures
  ComponentName component = intent.resolveActivity(context.getPackageManager());
  if (component != null) {
    File output = createImageFile(context);
    Uri uri = FileProvider.getUriForFile(
        context,
        BuildConfig.PROVIDER_AUTHORITY,
        output);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
    context.grantUriPermission(
        component.getPackageName(), 
        uri,
        Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
    context.startActivityForResult(intent, REQUEST_CAMERA);
  }
}

private File createImageFile(Context context) {
  File dir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "Camera");
  return File.createTempFile(
    "IMG_" + System.currentTimeMillis() + ".jpg",
    dir);
}

In this case the provider authority is com.pixplicity.example.fileprovider, which is specified in the manifest and in the build.gradle:

buildConfigField "String", "PROVIDER_AUTHORITY", "\"com.pixplicity.example.fileprovider\""