0

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.

Community
  • 1
  • 1
Mark
  • 7,446
  • 5
  • 55
  • 75
  • The "demo" the OP is referring to is: https://github.com/commonsguy/cw-advandroid/tree/master/Camera/Preview – CommonsWare Mar 25 '12 at 16:38
  • Hmm, I must apologize, because I can no longer reproduce the problem. Indeed, it seems to work first time every time now. I can only assume the generic "startPreview failed" RuntimeException was being caused by something else. Out of interest, the Advanced Android Development book talks about a bug in the Camera preview logic requiring the orientation in landscape mode. I'd be interested to know more about that? – Mark Mar 25 '12 at 16:55
  • At least through 1.x, you had to be in landscape for anything involving `Camera` to work. Off the top of my head, I do not recall when that was fixed. – CommonsWare Mar 25 '12 at 16:56

1 Answers1

1

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491