Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Camerax crash on real devices

I started to use CameraX (1.0.8 alpha) library in my Android application and during development on real Samnsung A50 device + emulators all working fine. But when it was release to Play Store - I see a lot of crashes on Pixel 2XL and Nexus 5X devices (I tried my app on emulators for this devices, but all is working fine).

I'm just call bindToLifecle:

Fatal Exception: java.lang.IllegalArgumentException: Can not get supported output size under supported maximum for the format: 34
   at androidx.camera.camera2.internal.SupportedSurfaceCombination.getSupportedOutputSizes(SupportedSurfaceCombination.java:29)
   at androidx.camera.camera2.internal.Camera2DeviceSurfaceManager.getSuggestedResolutions(Camera2DeviceSurfaceManager.java:29)
   at androidx.camera.core.CameraX.calculateSuggestedResolutions(CameraX.java:14)
   at androidx.camera.lifecycle.ProcessCameraProvider.bindToLifecycle(ProcessCameraProvider.java)

Does anybody had such issues?

Code for init:

@SuppressLint("RestrictedApi")
private void definePermissionsCallback() {
    allPermissionsCheck = Dexter.withActivity(this)
            .withPermissions(WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA)
            .withListener(new MultiplePermissionsListener() {
                @Override
                public void onPermissionsChecked(MultiplePermissionsReport report) {
                    if (report.areAllPermissionsGranted()) {
                        isFileStoragePermissionGiven = true;
                        isCameraPermissionGiven = true;

                        SharedPreferences sharedPreferences = getSharedPreferences(APPLICATION_SETTINGS, MODE_PRIVATE);
                        sharedPreferences.edit().putBoolean(ALLOW_CAMERA, true).apply();
                        findViewById(R.id.switch_camera).setEnabled(true);

                        cameraProviderFuture = ProcessCameraProvider.getInstance(MainActivity.this);
                        cameraProviderFuture.addListener(() -> {
                            try {
                                cameraProvider = (ProcessCameraProvider) cameraProviderFuture.get();
                                bindPreview();
                            } catch (ExecutionException | InterruptedException e) {
                                Crashlytics.logException(e);
                            }
                        }, ContextCompat.getMainExecutor(MainActivity.this));

                        return;
                    }...


void bindPreview() {
    cameraProvider.unbindAll();
    Preview preview = new Preview.Builder()
            .setTargetName("Preview")
            .build();

    preview.setPreviewSurfaceProvider(previewView.getPreviewSurfaceProvider());

    cameraSelector = new CameraSelector.Builder().requireLensFacing(lensFacing).build();
    cameraProvider.bindToLifecycle(this, cameraSelector, preview);
}
like image 876
VladislavLysov Avatar asked Nov 30 '25 22:11

VladislavLysov


1 Answers

This may not answer perfectly and I could be wrong, but give following a try:

The Preview instance you are using to bind to life cycle, has the Builder which allows setting either of target resolution or target aspect ratio with setTargetResoltion or setTargetAspectRation.

It calls out that if not set

If not set, the default selected resolution will be the best size match to the device's screen resolution, or to 1080p (1920x1080), whichever is smaller.

And

If not set, resolutions with aspect ratio 4:3 will be considered in higher priority.

Respectively.

Based on the error message

Can not get supported output size under supported maximum for the format

It looks like it's not able to get the output size that it's trying to find for the default values for certain devices. This is possible as the HAL implementation of the Camera is done by OEMs (like Nokia, Huawei etc) and can have support for different supported size. If you want to look at supported resolutions in a given device you can use this app: Camera2Api Probe

Pointer to how Camera X selects automatic resolution

TL;DR; While the API should provide this support implicitly, considering it in alpha, try to set the Aspect Ratio or the Target Resolution explicitly so it works for most of devices. To make it highly configurable you can query the supported resolution using this api

Note that, I have no link or ownership with the mentioned app Camera2Api, but I used it to query Camera2 information for devices in my job.

like image 183
Minhaz Avatar answered Dec 02 '25 15:12

Minhaz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!