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:
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:
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" />
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.
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.
You can use
List<Camera.Size> getSupportedPreviewSizes ()
And pick the size from the list
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.