'How to access external USB cameras using Android cameraX?

I was following this codelab to create a cameraX application .

It's working fine in my phone . But the app I am developing is for an android media player.Which doesn't have any inbuilt camera , only an external usb cam is attached .

This is my code to start the camera.

private void startCamera() {
    ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(this);
    Preview preview = new Preview.Builder().build();
    preview.setSurfaceProvider(viewFinder.getSurfaceProvider());
    imageCapture = new ImageCapture.Builder().build();
    cameraProviderFuture.addListener(() -> {
                try {
                    ProcessCameraProvider cameraProvider = cameraProviderFuture.get();
                    cameraProvider.unbindAll();
                    cameraProvider.bindToLifecycle(this, CameraSelector.DEFAULT_BACK_CAMERA,preview,imageCapture);
                } catch (ExecutionException e) {
                    e.printStackTrace();
                    Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
                }
                catch (IllegalArgumentException e){
                    Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
                }
            }, ContextCompat.getMainExecutor(this)
    );


}

it's throwing IllegalArgumentException saying no camera connected .

in cameraProvider.bindToLifecycle(this, CameraSelector.DEFAULT_BACK_CAMERA,preview,imageCapture);

only CameraSelector.DEFAULT_FRONT_CAMERA and CameraSelector.DEFAULT_BACK_CAMERA available.

How to access an external camera ?

Open Camera app from play store is working fine .



Solution 1:[1]

As of now CameraX does not support the external usb camera. Instead of CameraX you can use Camera 2 API.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Khushwant Pratap yadav