Search code examples
androidandroid-cameracommonsware

Camera PreviewDemo on HTC Hero


If I run this demo on the HTC Hero (CyanogenMod 6.1.0) I get a RuntimeException from the Camera.startPreview() method.

This seems to be the same problem described here:

Android Camera will not work. startPreview fails

In other words, you need to switch the width and height around when setting the preview size. Indeed this works but would then break the demo on other devices.

Now, I understand the purpose of the demo is to show how to get a Camera preview up and running (and so this problem is beyond its scope) but I'm wondering if there is a clean workaround for this since I don't want to put "if HTC_HERO" style logic in my code (anyway, I'm sure other devices will have similar behaviour)?

One hack might be to catch the exception and then re-invoke the setPreviewSize() method with swapped params, but I'm hoping to find a nicer way.


Solution

  • I don't want to put "if HTC_HERO" style logic in my code (anyway, I'm sure other devices will have similar behaviour)?

    To some extent, that is inevitable, if you are trying to reach 100% of devices running 100% of arbitrary ROM mods, because bugs will abound.

    One hack might be to catch the exception and then re-invoke the setPreviewSize() method with swapped params, but I'm hoping to find a nicer way.

    In theory, there should be some universal preview size code that determines the right size and configures the SurfaceView accordingly. If the code of the AOSP camera app is any indication, this code will be massive, and it's still unlikely to work everywhere (e.g., off-spec devices or ROM mods violating whatever assumptions that the authors of the "universal" solution considered).

    You may wish to examine projects like ZXing's Barcode Scanner and see how they approach the problem.