Video and Image Capture with CameraX in Android

<h1>Table of Contents</h1> <ul> <li>Permissions</li> <li>Image Capture<br /> - Save Photo<br /> - Capture Photo</li> <li>Video Capture</li> </ul> <h1>Getting Started</h1> <p>App level&nbsp;<code>build.gradle</code>&nbsp;file,</p> <pre> def camerax_version = &quot;1.3.0-alpha02&quot; implementation &quot;androidx.camera:camera-core:${camerax_version}&quot; implementation &quot;androidx.camera:camera-camera2:${camerax_version}&quot; implementation &quot;androidx.camera:camera-lifecycle:${camerax_version}&quot; implementation &quot;androidx.camera:camera-video:${camerax_version}&quot; implementation &quot;androidx.camera:camera-view:${camerax_version}&quot; implementation &quot;androidx.camera:camera-extensions:${camerax_version}&quot;</pre> <p><code>AndroidManifest.xml</code>&nbsp;for permissions,</p> <pre> &lt;uses-feature android:name=&quot;android.hardware.camera.any&quot; /&gt; &lt;uses-permission android:name=&quot;android.permission.CAMERA&quot; /&gt; &lt;uses-permission android:name=&quot;android.permission.RECORD_AUDIO&quot; /&gt; &lt;uses-permission android:name=&quot;android.permission.WRITE_EXTERNAL_STORAGE&quot; android:maxSdkVersion=&quot;28&quot; /&gt;</pre> <p>Adding&nbsp;<code>android.hardware.camera.any</code>&nbsp;makes sure that the device has a camera. Specifying&nbsp;<code>.any</code>&nbsp;means that it can be a front camera or a back camera.</p> <h1>Permissions</h1> <pre> class MainActivity : AppCompatActivity() { companion object { private const val REQUEST_CODE_PERMISSIONS = 10 private val REQUIRED_PERMISSIONS = mutableListOf ( Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO, ).apply { if (Build.VERSION.SDK_INT &lt;= Build.VERSION_CODES.P) { add(Manifest.permission.WRITE_EXTERNAL_STORAGE) } }.toTypedArray() } private lateinit var viewModel: SharedViewModel override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) viewModel = ViewModelProvider(this).get(SharedViewModel::class.java) if (!allPermissionsGranted()) { ActivityCompat.requestPermissions(this, REQUIRED_PERMISSIONS, REQUEST_CODE_PERMISSIONS) } else { viewModel.setPermission(allPermissionsGranted()) } //... } override fun onRequestPermissionsResult(requestCode: Int, permissions: Array&lt;out String&gt;, grantResults: IntArray) { super.onRequestPermissionsResult(requestCode, permissions, grantResults) if (requestCode == REQUEST_CODE_PERMISSIONS) { viewModel.setPermission(allPermissionsGranted()) } } private fun allPermissionsGranted() = REQUIRED_PERMISSIONS.all { ContextCompat.checkSelfPermission(this, it) == PackageManager.PERMISSION_GRANTED } }</pre> <p>We&rsquo;ll ask for necessary permissions on app launch but it&rsquo;s not ideal for production.</p> <p><strong><em>Recommended Implementation:</em></strong><em>&nbsp;Generally, you should display a UI explaining to the user the features that will be enabled if they grant permissions for the app to post notifications. This UI should provide the user options to agree or deny, such as&nbsp;</em><strong><em>OK</em></strong><em>&nbsp;and&nbsp;</em><strong><em>No thanks</em></strong><em>&nbsp;buttons. If the user selects&nbsp;</em><strong><em>OK</em></strong><em>, directly request the permission. If the user selects&nbsp;</em><strong><em>No thanks</em></strong><em>, allow the user to continue without notifications.</em></p> <p><a href="https://itnext.io/video-and-image-capture-with-camerax-in-android-797f497c0487">Visit Now</a></p>