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. Customizing ListAdapter


I have a ListActivity for which I am setting up ListAdapter like below:

        phoneListAdapter = new SimpleCursorAdapter(this,
                        managedCursor, new String[] { PhonesColumns.NUMBER,
                        new int[] {, });

However, the output here is not readable as PhonesColumns.TYPE is
displaying number strings like 2, 1 etc. A sample output that I get is
like below:

111-222-3333 2
222-333-4444 1

I want to make it more readable by converting them to "Mobile",
"Home", "Work" etc. A sample desired output list is like below:

111-222-3333 MOBILE
222-333-4444 WORK

Essentially, I don't want the columns to be displayed as is. How do I
achieve this ? My contacts_list_row.xml is like below:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android=""
<TextView android:id="@+id/text1"

<TextView android:id="@+id/text2"



2. Receive notification for onDestroy, onStop, and onPause etc in other applications?

Just to clarify: by this I meant to receive notification when
onDestroy, onStop etc in other applications are called, i.e. somehow
monitor the life cycle of apps running on the phone.


3. How To Start a RTSP connection

4. Problem with list.setOnItemSelectedListener (new OnItemSelectedListener() {});

5. Location and context aware service in Android

6. get the images from media/internal/images/media

7. diaplay images