startPreview failed but not all devices startPreview failed but not all devices android android

startPreview failed but not all devices


You seem to be looking for the minimal supported preview size. But if this size is not supported as picture size, your code will initialize the camera into an unsupported state. Some devices may choose to fail to open preview in such state.

I am not sure you really need picture size to be equal to preview frame size. If you don't, you can simply iterate separately on the values from parameters.getSupportedPictureSizes().

Another common mistake (not necessarily applicable to your use case) is rooted in that the sizes for rear-facing camera do not match those of the front-facing camera. Therefore, the size must always be recalculated when you switch the camera.


Question isnt using camera manager related library, so it isnt useful to question poster (its ancient I assume he is already fixed or dont care anymore to issue), but this answer may help or fix on user that facing issue startpreview error only on some various devices.

Causes

After deep debug, I found that camera manager library try to use largest suitable resolution if no exact size match.

As in CameraConfiguration.findBestPreviewSizeValue() :

// If no exact match, use largest preview size. This was not a great// idea on older devices because// of the additional computation needed. We're likely to get here on// newer Android 4+ devices, where// the CPU is much more powerful.if (!supportedPreviewSizes.isEmpty()) {    Camera.Size largestPreview = supportedPreviewSizes.get(0);    Point largestSize = new Point(largestPreview.width, largestPreview.height);    Log.i(TAG, "Using largest suitable preview size: " + largestSize);    return largestSize;}

This is good on the device's manufacture is properly set on the supported preview sizes.

But thing sometime happens! Device may report ridiculous high resolution which overwhelming the device on startPreview(), which an internal driver error occur 'preview timeout', only visible on catlog.

Lets fix it!

  • Use closest resolution to the screen instead!

Modifiying CameraConfiguration.java or maybe inside CameraConfigurationUtils.java:

... Skip lines until you find next pattern (take note that your source may be a slightly different!)

+ Add lines

- Remove lines

......public static Point findBestPreviewSizeValue(Camera.Parameters parameters, Point screenResolution) {......    // Find a suitable size, with max resolution    int maxResolution = 0;    Camera.Size maxResPreviewSize = null;  + double closestResolutionDrift = 1024*1024;  + Camera.Size closestResolution = null;    for (Camera.Size size : rawSupportedSizes) {......        Log.i(TAG, "Found preview size exactly matching screen size: " + exactPoint);        return exactPoint;    }  +   + double drift = (maybeFlippedWidth * maybeFlippedHeight)-(screenResolution.x * screenResolution.y);  + if (drift < closestResolutionDrift && resolution>(screenResolution.x * screenResolution.y)) {  +   closestResolutionDrift = drift;  +   closestResolution = size;  + }......    //If no exact match, use largest preview size. This was not a great idea on older devices because    //of the additional computation needed. We're likely to get here on newer Android 4+ devices, where    //the CPU is much more powerful.  - if (maxResPreviewSize != null) {  -      Point largestSize = new Point(maxResPreviewSize.width, maxResPreviewSize.height);  -     Log.i(TAG, "Using largest suitable preview size: " + largestSize);  -     return largestSize;  - }  + //if (maxResPreviewSize != null) {  + //     Point largestSize = new Point(maxResPreviewSize.width, maxResPreviewSize.height);  + //    Log.i(TAG, "Using largest suitable preview size: " + largestSize);  + //    return largestSize;  + //}  +   + // Dont! Some low end device still report ridiculous high resolution which overwhelming startPreview!  + if(closestResolution!=null){  +     Point closestSize = new Point(closestResolution.width, closestResolution.height);  +     Log.i(TAG, "Using closest suitable preview size: " + closestSize);  +     return closestSize;  + }