title: Preview in Custom Camera kotlin
body: <p>Designing custom camera. There was a problem with the Preview.
  I have 2 classes: <strong>CameraActivity, CameraPreview</strong>. Same as <a href="https://developer.android.com/guide/topics/media/camera" rel="nofollow noreferrer">here</a>.</p>
  
  <p>After capturing photo, the photo is saved, and the preview stops and nothing happens, but after clicking again, there is no photo, and the preview restores the work.</p>
  
  <p>How to fix it? Maybe i don't correctly use Preview Class? </p>
  
  <p><strong>CameraActivity.kt</strong></p>
  
  <pre><code>class CameraActivity(context: Context, frame : FrameLayout) {
  
      private var mCamera: Camera? = null       
      private lateinit var mPreview: SurfaceView
  
      init{
          setCamerasId()
          initCamera(mCameraIdBack) 
      }
      private fun getCameraInstance(id : Int): Camera? {
          return try {
              Camera.open(id)
          } catch (e: Exception) {null}
      }
  
      private fun initCamera(id : Int){
          mCamera = getCameraInstance(id)
          mPreview = CameraPreview(mContext, mCamera!!)
          mFrame.addView(mPreview)
      }
  
      fun takePhoto() {
          try {
              mCamera?.takePicture(null, null, mPicture)
          } catch (e : Exception){
              Toast.makeText(mContext, "Error capture photo", Toast.LENGTH_SHORT).show()
          }
          try {
              mCamera?.startPreview()
          } catch (e : Exception){
              Toast.makeText(mContext, "Error start preview", Toast.LENGTH_SHORT).show()
          }
  
          mPictureFile = null
      }
      private val mPicture = Camera.PictureCallback { data, _ -&gt;
          mPictureFile = getOutputMediaFile() ?: run {
              return@PictureCallback
          }
          try {
              Toast.makeText(mContext, "Success", Toast.LENGTH_SHORT).show()
              val fos = FileOutputStream(mPictureFile)
              fos.write(data)
              fos.close()
              addImageToGallery(mPictureFile!!.path, mContext)
              callShutterSound()
  
          } catch (e: Exception) {}
      }
  </code></pre>
  
  <p><strong>CameraPreview.kt</strong></p>
  
  <pre><code>class CameraPreview(
          private var mContext: Context,
          private var mCamera: Camera
  ) : SurfaceView(mContext), SurfaceHolder.Callback {
      private val mHolder: SurfaceHolder = holder.apply {
          addCallback(this@CameraPreview)
          setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS)
      }
  
      override fun surfaceCreated(holder: SurfaceHolder) {
          mCamera.apply {
              try {
                  setParameters()
                  setPreviewDisplay(holder)
                  startPreview()
              } catch (e: Exception) {}
          }
      }
      override fun surfaceDestroyed(holder: SurfaceHolder) {
          mCamera.release()
      }
  
      override fun surfaceChanged(holder: SurfaceHolder, format: Int, w: Int, h: Int) {
          if (mHolder.surface == null) {
              return
          }
          try {
              mCamera.stopPreview()
          } catch (e: Exception) {}
          mCamera.apply {
              try {
                  setParameters()
                  setPreviewDisplay(mHolder)
                  startPreview()
              } catch (e: Exception) {}
          }
      }
  }
  </code></pre>