James
James

Reputation: 33

Android SDK Camera API Demo Crashes

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

Upvotes: 3

Views: 16354

Answers (5)

Corey Trager
Corey Trager

Reputation: 23141

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:

https://android.googlesource.com/platform/packages/apps/Camera

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.

Upvotes: 4

Luis Miguel Serrano
Luis Miguel Serrano

Reputation: 5099

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.

Upvotes: 0

ee3509
ee3509

Reputation: 171

You can use

List<Camera.Size> getSupportedPreviewSizes ()

And pick the size from the list

Upvotes: 0

Ita
Ita

Reputation: 1292

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.

Upvotes: 0

JeremyFromEarth
JeremyFromEarth

Reputation: 14344

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" />

Upvotes: 7

Related Questions