title: android : camera preview using surfaceview
body: <pre><code>package com.example.preview;
  
  import android.app.Activity;
  import android.hardware.Camera;
  import android.os.Bundle;
  import android.util.Log;
  import android.view.SurfaceHolder;
  import android.view.SurfaceView;
  import android.view.View;
  import android.widget.Button;
  
  
  public class MainActivity extends Activity implements SurfaceHolder.Callback
  {
  Button start,stop;
  Camera camera;
  private final String tag = "VideoServer";
  SurfaceView surfaceView;
  SurfaceHolder surfaceHolder;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) 
  {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
       start = (Button)findViewById(R.id.button1);
       start.setOnClickListener(new Button.OnClickListener()
       {
           public void onClick(View arg0)
           {
            start_camera();
           }
       });
  
       stop = (Button)findViewById(R.id.button2);
       stop.setOnClickListener(new Button.OnClickListener()
       {
          public void onClick(View arg0)
          {
           stop_camera();
          }
       });
       surfaceView = (SurfaceView)findViewById(R.id.surfaceView1);
       surfaceHolder = surfaceView.getHolder();
       surfaceHolder.addCallback(this);
    // surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
  }
  
  
          private void start_camera() 
          {
  
              try
               {
                 camera = Camera.open();
               }
               catch(RuntimeException e)
               {
                 Log.e(tag, "init_camera: " + e);
                 return;
               }
               Camera.Parameters param;
               param = camera.getParameters();
               //modify parameter
              //param.setPreviewFrameRate(20);
               param.setPreviewSize(176,144);
               camera.setParameters(param);
               try 
                {
                  camera.setPreviewDisplay(surfaceHolder);
                  camera.startPreview();
                } 
               catch (Exception e)
               {
                  Log.e(tag, "init_camera: " + e);
                  return;
                }
              }
  
          private void stop_camera()
          {
           camera.stopPreview();
           camera.release();
          }
          public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
              // TODO Auto-generated method stub
              }
  
              public void surfaceCreated(SurfaceHolder holder) {
              // TODO Auto-generated method stub
              }
  
              public void surfaceDestroyed(SurfaceHolder holder) {
              // TODO Auto-generated method stub
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>This is the code just to preview on clicking start button.
  But there is some error. On clicking the preview is stopped is popped out. Any suggestions from any one will be appreciable. I have added every permissions that is required. Still its not working</p>
  
  <p>logcat error</p>
  
  <pre><code>11-28 11:01:49.621: E/AndroidRuntime(3558): FATAL EXCEPTION: main
  11-28 11:01:49.621: E/AndroidRuntime(3558): java.lang.NullPointerException
  11-28 11:01:49.621: E/AndroidRuntime(3558):     at    com.example.preview.MainActivity.start_camera(MainActivity.java:64)
  11-28 11:01:49.621: E/AndroidRuntime(3558):     at com.example.preview.MainActivity.access$0(MainActivity.java:51)
  11-28 11:01:49.621: E/AndroidRuntime(3558):     at com.example.preview.MainActivity$1.onClick(MainActivity.java:32)
  11-28 11:01:49.621: E/AndroidRuntime(3558):     at android.view.View.performClick(View.java:3511)
  11-28 11:01:49.621: E/AndroidRuntime(3558):     at android.view.View$PerformClick.run(View.java:14109)
  11-28 11:01:49.621: E/AndroidRuntime(3558):     at android.os.Handler.handleCallback(Handler.java:605)
  11-28 11:01:49.621: E/AndroidRuntime(3558):     at android.os.Handler.dispatchMessage(Handler.java:92)
  11-28 11:01:49.621: E/AndroidRuntime(3558):     at android.os.Looper.loop(Looper.java:137)
  11-28 11:01:49.621: E/AndroidRuntime(3558):     at android.app.ActivityThread.main(ActivityThread.java:4424)
  11-28 11:01:49.621: E/AndroidRuntime(3558):     at java.lang.reflect.Method.invokeNative(Native Method)
  11-28 11:01:49.621: E/AndroidRuntime(3558):     at java.lang.reflect.Method.invoke(Method.java:511)
  11-28 11:01:49.621: E/AndroidRuntime(3558):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
  11-28 11:01:49.621: E/AndroidRuntime(3558):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
  11-28 11:01:49.621: E/AndroidRuntime(3558):     at dalvik.system.NativeStart.main(Native Method)
  </code></pre>