views:

2443

answers:

5

The Android SDK has an API demo for using the preview of the camera. However, this gives me a runtime exception in the emulator. I'm running with Eclipse on a Mac with 10.6

Here's the link where I grabbed the code:

http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/CameraPreview.html

+2  A: 

Make sure you have set permissions in the AndroidManifest.xml file for using the camera. Place this line above the application tag.

<uses-permission android:name="android.permission.CAMERA" />
jeremynealbrown
thanks a bunch, I wish they had mentioned that in the demo code.
James
Camera Preview crashes for me with no useful info, even in the debugger, when running on an actual Nexus One device. The uses-permission line is already in the manifest.
Corey Trager
A: 

Regarding the NexusOne crash.

As Corey Trager mentioned in his comment, the missing permission is not the issue when looking into the ApiDemos package that comes with the 2.1 SDK.

One solution I've found to this issue is simply changing the requested preview size in CameraPreview

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h){..}

replace this line:

parameters.setPreviewSize(w,h);

with

parameters.setPreviewSize(352,288);

This resolution is the legal resolution that the G1 DevPhone works with.

Ita
+1  A: 

I got things to work on my Nexus One.

If you are trying to write a Camera app, the API demo app won't be near enough help. Download the source code for the Android Camera app itself:

http://android.git.kernel.org/?p=platform/packages/apps/Camera.git

Regarding your question, what you'll learn is that you will need to restrict your app to landscape mode in your manifest. In SurfaceChanged, just call startPreview. Don't set the preview size at all.

Corey Trager
A: 

You can use

List<Camera.Size> getSupportedPreviewSizes ()

And pick the size from the list

ee3509
A: 

This issue is also mentioned in Google Code Android defects, and it might be helpful to you, the code mentioned at the bottom in one of the comments, as a possible solution to deal with the preview problems.

Luis Miguel