Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8143557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:09:01+00:00 2026-06-06T13:09:01+00:00

I am working on the camera code in android to take picture and save

  • 0

I am working on the camera code in android to take picture and save it on the phone. It takes the picture from phone camera and saves it on the memory card. The only problem is that the camera preview does not restart after taking the picture.

I cannot figure out the solution. Code is as follows. Suggestions are needed . . .
There are two classes in my project . . .

CAMERAACTIVITY CLASS

public class CameraActivity extends Activity 
{

  private static final String TAG = "CameraDemo";
  Preview preview; 
  Button buttonClick; 

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    preview = new Preview(this); 
    ((FrameLayout) findViewById(R.id.preview)).addView(preview); 

    buttonClick = (Button) findViewById(R.id.buttonClick);
    buttonClick.setOnClickListener(new OnClickListener() {
      public void onClick(View v) { 
        preview.camera.takePicture(shutterCallback, rawCallback, jpegCallback);    
      }
    });

    Log.d(TAG, "onCreate'd");
  }

  // Called when shutter is opened
  ShutterCallback shutterCallback = new ShutterCallback() { 
    public void onShutter() {
      Log.d(TAG, "onShutter'd");
    }
  };

  // Handles data for raw picture
  PictureCallback rawCallback = new PictureCallback() {
    public void onPictureTaken(byte[] data, Camera camera) {
      Log.d(TAG, "onPictureTaken - raw");
    }
  };

  // Handles data for jpeg picture
  PictureCallback jpegCallback = new PictureCallback() { 
    public void onPictureTaken(byte[] data, Camera camera) {
      FileOutputStream outStream = null;
      try {
        // Write to SD Card
        outStream = new FileOutputStream(String.format("/sdcard/DCIM/queries.jpg",
            System.currentTimeMillis()));
        outStream.write(data);
        outStream.close();
        Log.d(TAG, "onPictureTaken - wrote bytes: " + data.length);
      } catch (FileNotFoundException e) { 
        e.printStackTrace();
      } catch (IOException e) {
        e.printStackTrace();
      } finally {
      }
      Log.d(TAG, "onPictureTaken - jpeg");

    }
  };

}

Preview Class

class Preview extends SurfaceView implements SurfaceHolder.Callback{ 
  private static final String TAG = "Preview";

  SurfaceHolder mHolder;  // <2>
  public Camera camera; // <3>

  Preview(Context context) {
    super(context);

    // Install a SurfaceHolder.Callback so we get notified when the
    // underlying surface is created and destroyed.
    mHolder = getHolder();  // <4>
    mHolder.addCallback(this);  // <5>
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); // <6>

  }

  // Called once the holder is ready
  public void surfaceCreated(SurfaceHolder holder) {  // <7>
    // The Surface has been created, acquire the camera and tell it where
    // to draw.
    camera = Camera.open(); // <8>
    try {


        Camera.Parameters parameters = camera.getParameters();
        parameters.set("orientation", "landscape");
        camera.setParameters(parameters);
        camera.setPreviewDisplay(holder);  

        camera.setPreviewCallback(new PreviewCallback() { 
        // Called for each frame previewed
        public void onPreviewFrame(byte[] data, Camera camera) {  
          Log.d(TAG, "onPreviewFrame called at: " + System.currentTimeMillis());
          Preview.this.invalidate();  
        }
      });
    } catch (IOException e) { 
      e.printStackTrace();
    }

  }

  // Called when the holder is destroyed
  public void surfaceDestroyed(SurfaceHolder holder) { 
    //Log.d(TAG,"Stopping preview in SurfaceDestroyed().");
    camera.setPreviewCallback(null);
    camera.stopPreview();
    camera.release();
    camera = null;           
  }

  // Called when holder has changed
  public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { 
    camera.startPreview();
  }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T13:09:03+00:00Added an answer on June 6, 2026 at 1:09 pm

    The article about Camera from Android API Guide also suffers from the same problem. I could get the preview back after taking pictures by adding two more stuff like following:

    1) Add external storage permission in the AndroidManifest.xml file:

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    

    2) Start preview again by calling camera.startPreview(). In your code:

    ...
    Log.d(TAG, "onPictureTaken - jpeg");
    camera.startPreview();
    ....
    

    I’m sure you’ll be able to get yours to work in the same way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on an application, where i take pictures from a camera and
Ok so here is the problem, I found a nice working 2D Camera from:
I want to stream the video from android phone camera to VLC media player
I am working with Augmented Reality application with android, and I implemented camera code.
I am working on an Android app that uses the phone's camera and I'm
I'm working on a security camera application using the webcam that takes a photo
I'm working on a little Android app to stream some camera footage (as a
I'm working on a realtime imaging system. UI thread: grabs images from a camera
I have a piece of code that aims to launch the android camera, let
I am trying to implement a camera controlled scene using the camera code from:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.