title: how to display image in imageview after captured using surfaceview class in android
body: <p>i have code that capture the image using custom camera API, the problem is its not coming back to preview mode, main thing is i want display the  captured image in imageview"    </p>
  
  <p><strong>this is my code:</strong></p>
  
  <p>CameraPreview.java</p>
  
  <pre><code>import java.io.IOException;
  import android.content.Context;
  import android.hardware.Camera;
  import android.hardware.Camera.AutoFocusMoveCallback;
  import android.view.SurfaceHolder;
  import android.view.SurfaceView;
  
  public class CameraPreview extends SurfaceView implements
          SurfaceHolder.Callback {
      private SurfaceHolder mSurfaceHolder;
      private Camera mCamera;
  
      // Constructor that obtains context and camera
      @SuppressWarnings("deprecation")
      public CameraPreview(Context context, Camera camera) {
          super(context);
          this.mCamera = camera;
          this.mSurfaceHolder = this.getHolder();
          this.mSurfaceHolder.addCallback(this);
          this.mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
  
      }@Override
      public void surfaceCreated(SurfaceHolder surfaceHolder) {
          try {
              mCamera.setPreviewDisplay(surfaceHolder);
              mCamera.startPreview();
              mCamera.setDisplayOrientation(90);
              mCamera.startPreview();
  
              Camera.Parameters prams = mCamera.getParameters();
              prams.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
  
              mCamera.setParameters(prams);
              mCamera.setPreviewDisplay(surfaceHolder);
              mCamera.startPreview();
              mCamera.autoFocus(null);
  
          } catch (IOException e) {
              // left blank for now
          }
      }@Override
      public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
          mCamera.stopPreview();
          mCamera.release();
  
      }
  
      @Override
      public void surfaceChanged(SurfaceHolder surfaceHolder, int format,
              int width, int height) {
  
  
  
  
  
          // start preview with new settings
          try {
              mCamera.setPreviewDisplay(surfaceHolder);
              mCamera.startPreview();
          } catch (Exception e) {
              // intentionally left blank for a test
          }
      }
  </code></pre>
  
  <p>}</p>
  
  <p><strong>MainActivity.java</strong></p>
  
  <pre><code>import java.io.File;
  import java.io.FileNotFoundException;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.text.SimpleDateFormat;
  import java.util.Date;
  import android.app.Activity;
  import android.hardware.Camera;
  import android.hardware.Camera.AutoFocusCallback;
  import android.hardware.Camera.PictureCallback;
  import android.os.Bundle;
  import android.os.Environment;
  import android.util.Log;
  import android.view.View;
  import android.widget.Button;
  import android.widget.FrameLayout;
  import android.widget.ImageView;
  public class MainActivity extends Activity {
  
      private static final String TAG = MainActivity.class.getName();
      private Camera mCamera;
      private CameraPreview mCameraPreview;
      private Button captureButton;
      private ImageView showImageView;
  
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          mCamera = getCameraInstance();
          mCameraPreview = new CameraPreview(this, mCamera);
  
          FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
  
          ImageView showImageView = (ImageView) findViewById(R.id.imageView1);
  
          preview.addView(mCameraPreview);
  
  
          captureButton = (Button) findViewById(R.id.button_capture);
  
          captureButton.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  mCamera.takePicture(null, null, mPicture);
              }
          });
  
      }
  private Camera getCameraInstance() {
          Camera camera = null;
          try {
              camera = Camera.open();
  
          } catch (Exception e) {
              // cannot get camera or does not exist
          }
          return camera;
      }
  
      PictureCallback mPicture = new PictureCallback() {
          @Override
          public void onPictureTaken(byte[] data, Camera camera) {
              File pictureFile = getOutputMediaFile();
              if (pictureFile == null) {
                  return;
              }
              try {
                  FileOutputStream fos = new FileOutputStream(pictureFile);
                  fos.write(data);
                  fos.close();
              } catch (FileNotFoundException e) {
  
              } catch (IOException e) {
              }
          }
      };
  
      private static File getOutputMediaFile() {
          File mediaStorageDir = new File(
                  Environment
                          .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                  "MyCameraApp");
          if (!mediaStorageDir.exists()) {
              if (!mediaStorageDir.mkdirs()) {
                  Log.d("MyCameraApp", "failed to create directory");
                  return null;
              }
          }
          // Create a media file name
          String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
                  .format(new Date());
          File mediaFile;
          mediaFile = new File(mediaStorageDir.getPath() + File.separator
                  + "IMG_" + timeStamp + ".jpg");
  
          return mediaFile;
      }
  }
  </code></pre>
  
  <p><strong>activity_main.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/RelativeLayout1"
      android:layout_width="fill_parent"
      android:layout_height="match_parent"
      android:gravity="center" &gt;
  
      &lt;FrameLayout
          android:id="@+id/camera_preview"
          android:layout_width="fill_parent"
          android:layout_height="200dp"
          android:layout_above="@+id/btns"
          android:layout_alignParentTop="true" &gt;
  
      &lt;/FrameLayout&gt;
  
      &lt;RelativeLayout
          android:id="@+id/btns"
          android:layout_width="fill_parent"
          android:layout_height="100dp"
          android:layout_alignParentBottom="true"
          android:layout_alignParentLeft="true"
          android:background="#ccc" &gt;
  
          &lt;Button
              android:id="@+id/button_capture"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_centerHorizontal="true"
              android:layout_centerVertical="true"
              android:layout_gravity="center"
              android:background="@drawable/btn_white" /&gt;
  
          &lt;ImageView
              android:id="@+id/imageView1"
              android:layout_width="50dp"
              android:layout_height="50dp"
              android:layout_alignParentLeft="true"
              android:layout_centerVertical="true"
              android:layout_marginLeft="22dp"
              android:src="@drawable/backeff" /&gt;
  
      &lt;/RelativeLayout&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>