title: CameraX View in React Native - Unable to configure camera
body: <p>I am creating cameraX view for react native through bridging.(Normal Views are rendering in RN. Bridging flow is fine.) Using <code>PreviewView</code> for display camera feed for CameraX’s preview. I have successfully implemented the preview but not sure why preview is not rendering on the UI. <strong>It just a blank screen.</strong></p>
  <p>Let me know the right way of implementation for UI native component. I have tired samething in activity and working fine. I am new to the android platform. Thanks in advance!</p>
  <p><a href="https://groups.google.com/a/android.com/g/camerax-developers/c/G9jKs1Bo_CE" rel="noreferrer">https://groups.google.com/a/android.com/g/camerax-developers/c/G9jKs1Bo_CE</a>
  Checked this. But not sure exactly if the problem is with the re-layout.</p>
  <p>Log from NativeCameraView class:</p>
  <pre><code>23:21:45.415 8026-8026/com.app D/PreviewView: Surface requested by Preview.
  11-11 23:21:45.415 8026-8784/com.app D/UseCaseAttachState: All use case: [androidx.camera.core.Preview-ee9dbc80-160c-45e5-a38f-e68bc8f54373, androidx.camera.core.Preview-3e33af92-3cb6-4cb4-a0c3-dfb4e3c6cc8d] for camera: 0
  11-11 23:21:45.415 8026-8784/com.app D/UseCaseAttachState: Active and attached use case: [androidx.camera.core.Preview-3e33af92-3cb6-4cb4-a0c3-dfb4e3c6cc8d] for camera: 0
  11-11 23:21:45.416 8026-8784/com.app D/UseCaseAttachState: All use case: [androidx.camera.core.Preview-ee9dbc80-160c-45e5-a38f-e68bc8f54373, androidx.camera.core.Preview-3e33af92-3cb6-4cb4-a0c3-dfb4e3c6cc8d] for camera: 0
  11-11 23:21:45.416 8026-8784/com.app D/UseCaseAttachState: Active and attached use case: [androidx.camera.core.Preview-ee9dbc80-160c-45e5-a38f-e68bc8f54373, androidx.camera.core.Preview-3e33af92-3cb6-4cb4-a0c3-dfb4e3c6cc8d] for camera: 0
  11-11 23:21:45.417 8026-8784/com.app D/UseCaseAttachState: Active and attached use case: [androidx.camera.core.Preview-ee9dbc80-160c-45e5-a38f-e68bc8f54373, androidx.camera.core.Preview-3e33af92-3cb6-4cb4-a0c3-dfb4e3c6cc8d] for camera: 0
  11-11 23:21:50.459 8026-8785/com.app E/Camera2CameraImpl: Unable to configure camera 0, timeout!
  </code></pre>
  <p>Log from Camera Activity:</p>
  <pre><code>11-12 22:55:40.545 6445-6445/com.app D/ReactVideoEvents: onHostPause
  11-12 22:55:40.592 6445-7585/com.app D/UseCaseAttachState: All use case: [androidx.camera.core.Preview-62b1221e-ac9f-4bc8-a145-f0dcb1f2e162] for camera: 0
  11-12 22:55:40.593 6445-7585/com.app D/UseCaseAttachState: Active and attached use case: [] for camera: 0
  11-12 22:55:40.593 6445-7585/com.app D/UseCaseAttachState: All use case: [androidx.camera.core.Preview-62b1221e-ac9f-4bc8-a145-f0dcb1f2e162] for camera: 0
  11-12 22:55:40.601 6445-7585/com.app I/CameraManager: Using legacy camera HAL.
  11-12 22:55:40.604 6445-6445/com.app D/PreviewView: Surface requested by Preview.
  11-12 22:55:40.611 6445-6445/com.app D/TextureViewImpl: SurfaceTexture available. Size: 1440x1080
  11-12 22:55:40.612 6445-6445/com.app D/TextureViewImpl: Surface set on Preview.
  11-12 22:55:40.791 6445-7585/com.app D/UseCaseAttachState: Active and attached use case: [androidx.camera.core.Preview-62b1221e-ac9f-4bc8-a145-f0dcb1f2e162] for camera: 0
  11-12 22:55:40.793 6445-7585/com.app D/UseCaseAttachState: All use case: [androidx.camera.core.Preview-62b1221e-ac9f-4bc8-a145-f0dcb1f2e162] for camera: 0
  11-12 22:55:40.793 6445-7585/com.app D/CaptureSession: Opening capture session.
  11-12 22:55:40.795 6445-7585/com.app I/CameraDeviceState: Legacy camera service transitioning to state CONFIGURING
  11-12 22:55:40.795 6445-8660/com.app I/RequestThread-0: Configure outputs: 1 surfaces configured.
  11-12 22:55:40.795 6445-8660/com.app D/Camera: app passed NULL surface
  11-12 22:55:40.819 6445-7585/com.app I/CameraDeviceState: Legacy camera service transitioning to state IDLE
  11-12 22:55:40.820 6445-7578/com.app D/CaptureSession: Attempting to send capture request onConfigured
  11-12 22:55:40.820 6445-7578/com.app D/CaptureSession: Issuing request for session.
  11-12 22:55:40.822 6445-7578/com.app I/RequestQueue: Repeating capture request set.
  11-12 22:55:40.822 6445-7578/com.app D/CaptureSession: CameraCaptureSession.onConfigured() mState=OPENED
  11-12 22:55:40.823 6445-7585/com.app D/CaptureSession: CameraCaptureSession.onReady() OPENED
  </code></pre>
  <p>Below is the snippet:</p>
  <pre><code>public class myNativeCameraView extends FrameLayout {
  ...
  
  public myNativeCameraView(ThemedReactContext context) {
      super(context);
      Log.d(&quot;ReactSpoCameraManager&quot;, &quot;myNativeCameraView 1&quot;);
      mContext = context;
  
      FrameLayout layout = (FrameLayout) LayoutInflater.from(context).inflate(R.layout.camera_layout, null);
      mPreviewView = layout.findViewById(R.id.previewView);
      startCamera();
      setupLayoutHack();
  }
  
  private void startCamera() {
      final ListenableFuture&lt;ProcessCameraProvider&gt; cameraProviderFuture = ProcessCameraProvider.getInstance(mContext);
      cameraProviderFuture.addListener(new Runnable() {
          @Override
          public void run() {
              try {
                  ProcessCameraProvider cameraProvider = cameraProviderFuture.get();
                  bindPreview(cameraProvider);
              } catch (ExecutionException | InterruptedException e) {}
          }
      }, ContextCompat.getMainExecutor(mContext));
  }
  
  void bindPreview(@NonNull ProcessCameraProvider cameraProvider) {
  mPreviewView.setPreferredImplementationMode(PreviewView.ImplementationMode.SURFACE_VIEW);
      Preview preview = new Preview.Builder().build();
      CameraSelector cameraSelector = new CameraSelector.Builder()
              .requireLensFacing(CameraSelector.LENS_FACING_BACK)
              .build();
      ImageAnalysis imageAnalysis = new ImageAnalysis.Builder()
              .build();
      ImageCapture.Builder builder = new ImageCapture.Builder();
  
      preview.setSurfaceProvider(mPreviewView.createSurfaceProvider());
      Camera camera = cameraProvider.bindToLifecycle((AppCompatActivity) mContext.getCurrentActivity(), cameraSelector, preview, imageAnalysis);
  }
  }
  </code></pre>
  <p>I am calling this <code>setupLayoutHack</code> after <code>startCamera</code> in constructor. To make sure if the problem is with re-layout in React Native. But Still facing same error.</p>
  <pre><code>void setupLayoutHack() {
          Choreographer.getInstance().postFrameCallback(new Choreographer.FrameCallback() {
              @Override
              public void doFrame(long frameTimeNanos) {
  //                Log.d(TAG, &quot;setupLayoutHack&quot;);
                  manuallyLayoutChildren();
                  getViewTreeObserver().dispatchOnGlobalLayout();
                  Choreographer.getInstance().postFrameCallback(this);
              }
          });
      }
  
      void manuallyLayoutChildren() {
          for (int i = 0; i &lt; getChildCount(); i++) {
              Log.d(TAG, &quot;manuallyLayoutChildren&quot;);
              View child = getChildAt(i);
              child.measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY),
                      MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY));
              child.layout(0, 0, child.getMeasuredWidth(), child.getMeasuredHeight());
          }
      }
  </code></pre>