Camera Preview

by roscoe_x » Mon, 15 Jun 2009 04:35:19 GMT

Sponsored Links
 Camera Preview sample code in here: 

My questions are:
1. There's an error in the program, in the try{} catch{} part in
surfaceCreated(). Any suggestions on how to solve this problem?
2. In the top part of the html page written "The file containing the
source code shown below is located in the corresponding directory in
<sdk>/platforms/android-<version>/samples/...". But I can not find the
sample code in that directory. Is the information wrong or my system
is incomplete?
3. What do I get (see) when the program run normally?

Thanks for the answer.


Camera Preview

by pat » Wed, 17 Jun 2009 01:49:36 GMT

 Hi everybody,

I implemented a camera preview SurfaceView that is loosely based on
the camera preview example from the android apis demo.

On the same activity, i have some live sensor readings displayed.

I always had the idea that the activity is running very slow and i
previously got some image freezings when I switched between two
different activities that both displayed the camera preview. That was
when I was building for SDK 1.1 today I migrated my application to 1.5
and catched the following error message:

06-15 23:56:26.272: ERROR/AndroidRuntime(726):
java.lang.RuntimeException: Out of memory
06-15 23:56:26.272: ERROR/AndroidRuntime(726):     at
android.hardware.Camera.native_setup(Native Method)
06-15 23:56:26.272: ERROR/AndroidRuntime(726):     at
06-15 23:56:26.272: ERROR/AndroidRuntime(726):     at

For clarity, I will post the code of my Preview class at the end.

Any idea what could be wrong?

Thanks a lot for your replies.


package nl.uva.graffiti;

import android.content.Context;
import android.hardware.Camera;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public classPreview extends SurfaceView implements

    SurfaceHolder mHolder;
    Camera mCamera;
    Context mContext;

    public Preview(Context context) {

    public Preview (Context context, AttributeSet attrs){
        super(context, attrs);
        mContext = context;

    public Preview(Context context, AttributeSet attrs, int defStyle){
        super(context, attrs, defStyle);

    private void init(){
        mHolder = getHolder();

    public void surfaceCreated(SurfaceHolder holder) {
        mCamera =;
        try {
        } catch (Exception exception) {
            mCamera = null;

    public void surfaceDestroyed(SurfaceHolder holder) {
        mCamera = null;

    public void surfaceChanged(SurfaceHolder holder, int format, int
w, int h) {
        Camera.Parameters parameters = mCamera.getParameters();
        parameters.setPreviewSize(w, h);



Sponsored Links

Camera preview

by tinyang » Fri, 28 Aug 2009 03:43:28 GMT


I just got my camera preview working for my app, but it is not
where I want it to appear, and I'm not sure how to get it there.
Instead of
using the entire screen for the preview, I would like to put it inside
surfaceview in an activity xml gui.  What do I need to change?  Here
is my

public class TakePic extends Activity {

SurfaceView camSurface;

Preview camPreview;


protected void onCreate(Bundle savedInstanceState) {


requestWindowFeature(Window.FEATURE_NO_TITLE);//hide window title

camPreview = new Preview(this); //create preview


setContentView(camPreview); //set preview as activity content

camSurface = (SurfaceView) findViewById(;


Camera preview

by Pavel » Fri, 28 Aug 2009 16:00:12 GMT

 Hey tinyang,

Could you please share the code you use for picture taking? I am
working on the same thing and having weird issues with it - it does
not take full sized pictures for unknown reason...

And yeah, sorry, no answer to your question from my side at the moment.



Camera preview

by tinyang » Sat, 29 Aug 2009 03:50:52 GMT

 Hi Pavel, and thanks for the reply.  I'm happy to share the snippet of
code I have for picture taking, but I have not tried it out yet
because in order to see and use the picture-taking button in my
activity view, I need to get the camera preview in the correct place

I still really need someone to help me on this please!! :)

Here is the code I was looking at trying, but I've done extensive
research on the subject trying to fix my placement of the camera
preview, so here are also some links that may help you with taking the
picture and storing it properly: (HTH!) #704 

public class ImageCaptureCallback implements PictureCallback  {

            private OutputStream filoutputStream;
            public ImageCaptureCallback(OutputStream filoutputStream) {
                this.filoutputStream = filoutputStream;
            public void onPictureTaken(byte[] data, Camera camera) {
                try {
                        Log.v(getClass().getSimpleName(), "onPictureTaken=" + 
data + "
length = " + data.length);
                        } catch(Exception ex) {


Camera preview

by Bill » Sat, 29 Aug 2009 09:23:13 GMT

 There seems to be a bug in the EXTRA_OPTIONS if you're using the
default camera activity. That is why you are not getting full sized

If you want to embed the preview you can use the Camera object


Camera preview

by tinyang » Mon, 31 Aug 2009 21:42:55 GMT

 Thank you Bill for your response!  This has been confounding me for a
while and I've had no idea how to remedy it.  Is it possible for you
to please include a code sample or a link to a code sample?


Re: Camera preview

by Mark Murphy » Fri, 11 Feb 2011 20:20:56 GMT

 That example has a flaw -- it hard-wires in /sdcard as the location of
external storage. Use File.getExternalStorageDirectory() for that
instead, or some other spot.

Here is another sample project:

This one is more complex, because it uses a front-facing camera on
Android 2.3+, but otherwise it does the same basic things as the
example you cited.

Mark Murphy (a Commons Guy)  |  | 

Android App Developer Books: 


Other Threads

1. android 1.5r2 manifest fixed

We recently noticed a bug we've had in the android_1.5r2 manifest
where it was using the latest version of cupcake instead of one
specific version.

I've just deployed a corrected version of that manifest so that it
permanently matches the android_1.5r2 code. If you've been working
from android_1.5r2 and synced your client from the Android Open-Source
Project since early July, you should sync again to get back to the
proper version.

Apologies about the inconvenience. I'll be changing our process in the
future to avoid such situations.


(PS: the android-1.5r2 tag on the manifest is currently incorrect,
I'll try to make it point to the right commit. The tags on the
individual projects are correct).

Jean-Baptiste M. "JBQ" Queru
Software Engineer, Android Open-Source Project, Google.

Questions sent directly to me that have no reason for being private
will likely get ignored or forwarded to a public forum with no further


2. Unable to unlock using keyboard in Android in the desktop screen

You need to have a key map file that describes which key generates the menu
key code, so you can press it to unlock.  Or modify the keyguard (the phone
policy in frameworks/policies/base) to do something more appropriate for
your hardware.

Dianne Hackborn
Android framework engineer

Note: please don't send private questions to me, as I don't have time to
provide private support, and so won't reply to such e-mails.  All such
questions should be posted on public forums, where I and others can see and
answer them.


3. Silent install on first boot

4. ListView: invisible dividers still take up space. Is it a bug or a feature?

5. how to implement OTA service ?

6. Multicast receive broken in Android 1.5?

7. ListView: invisible dividers still take up space. Is it a bug or a feature?