title: Android - Unable to start Camera from Floating View / Service - Kotlin
body: <p>I'm attempting to create a Floating View (somewhat like Facebook Messenger's floating user image)</p>
  <p><em><strong>Example:</strong></em>
  <a href="https://www.cnet.com/a/img/YcbyFcLt1YoAYIvkKOLIvUqDZbU=/1200x630/2013/04/12/5b94f150-f0e0-11e2-8c7c-d4ae52e62bcc/Facebook_Messenger_ChatHeads.jpg" rel="nofollow noreferrer">https://www.cnet.com/a/img/YcbyFcLt1YoAYIvkKOLIvUqDZbU=/1200x630/2013/04/12/5b94f150-f0e0-11e2-8c7c-d4ae52e62bcc/Facebook_Messenger_ChatHeads.jpg</a></p>
  <p><a href="https://i.stack.imgur.com/YclPW.jpg" rel="nofollow noreferrer"></a></p>
  <p><strong>Problem:</strong></p>
  <p>When I attempt to do so using the following code - the Camera preview is never started/populated/displayed, and I'm not sure why.</p>
  <p>Any suggestions/tips/ideas are appreciated.</p>
  <p><strong>Source Snippet:</strong></p>
  <pre><code>class FloatingViewService : Service() {
  
      private val context: Context? = null
      private val mView: View? = null
      private var mParams: WindowManager.LayoutParams? = null
      private var mWindowManager: WindowManager? = null
  
      private lateinit var binding: OverlayLayoutBinding
      private var imageCapture: ImageCapture? = null
  
      private var mFloatingView: View? = null
      private var params: WindowManager.LayoutParams? = null
      override fun onBind(intent: Intent?): IBinder? {
          return null
      }
  
      @SuppressLint(&quot;ClickableViewAccessibility&quot;)
      override fun onCreate() {
          super.onCreate()
          if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.O) startMyOwnForeground() else startForeground(
              1,
              Notification()
          )
          //Inflate the floating view layout we created
          mFloatingView = LayoutInflater.from(this).inflate(R.layout.overlay_layout, null)
          val LAYOUT_FLAG: Int
              LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
              params = WindowManager.LayoutParams(
                  WindowManager.LayoutParams.WRAP_CONTENT,
                  WindowManager.LayoutParams.WRAP_CONTENT,
                  LAYOUT_FLAG,
                  WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                  PixelFormat.TRANSLUCENT
              )
          
          startCamera()
  
          mWindowManager = getSystemService(WINDOW_SERVICE) as WindowManager?
          mWindowManager!!.addView(mFloatingView, params)
  
          startCamera()
  
      }
  
      private fun startCamera() {
  
          val cameraProviderFuture = ProcessCameraProvider
              .getInstance(this)
  
          cameraProviderFuture.addListener({
  
              val cameraProvider: ProcessCameraProvider = cameraProviderFuture.get()
  
              val preview = Preview.Builder()
                  .build()
                  .also { mPreview -&gt;
                      mPreview.setSurfaceProvider(
                          binding.viewFinder.surfaceProvider
                      )
                  }
              imageCapture = ImageCapture.Builder()
                  .build()
  
              val cameraSelector = CameraSelector.DEFAULT_FRONT_CAMERA
  
              try {
                  cameraProvider.unbindAll()
                  cameraProvider.bindToLifecycle(
                      (context as LifecycleOwner),
                      cameraSelector,
                      preview, imageCapture
                  )
  
              } catch (e: Exception) {
                  Log.d(Constants.TAG, &quot;Camera Failed to Start&quot;)
              }
          }, ContextCompat.getMainExecutor(this))
      }
  
  }
  </code></pre>
  <p><strong>Full Source:</strong></p>
  <p><a href="https://pastebin.com/3wG1QP98" rel="nofollow noreferrer">https://pastebin.com/3wG1QP98</a></p>
  <p><a href="https://drive.google.com/file/d/1ClgD3HLZaQ25toalygAZphuR7XUA3QRG/view?usp=sharing" rel="nofollow noreferrer">https://drive.google.com/file/d/1ClgD3HLZaQ25toalygAZphuR7XUA3QRG/view?usp=sharing</a></p>