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 6918513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:52:08+00:00 2026-05-27T09:52:08+00:00

I have an application that I have taking a picture, and then it is

  • 0

I have an application that I have taking a picture, and then it is supposed to send the data from the picture to another activity using an intent.

I am trying to call the intent inside the jpegCallback, but the problem is I also need to release the camera through the preview class before calling the intent. However, I can not get to the original preview object from inside the callback, so I need a way to call MainActivity.doPictureResults() from inside the callback. Or I need a way to have a listener that fires after all of the picture callbacks are done.

Here is my MainActivity class which holds an instance of Preview class in the mPreview variable. The jpegCallback is at the bottom, and I want to call the doPictureResults from inside that, or setup another callback for after that function is done.

public class MainActivity extends Activity {

    private final String TAG = "MainActivity";
    private Preview mPreview;
    Camera mCamera;
    int numberOfCameras;
    int cameraCurrentlyLocked;

    //The first rear facing camera
    int defaultCameraId;

    /**
     * Constructor
     * @param savedInstanceState 
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {Log.e(TAG, "onCreate");
        super.onCreate(savedInstanceState);

        //Hide the window title.
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

        //Create a RelativeLayout container that will hold a SurfaceView,
        //and set it as the content of our activity.
        this.mPreview = new Preview(this);
        setContentView(this.mPreview);

        //Find the total number of cameras available
        this.numberOfCameras = Camera.getNumberOfCameras();

        //Find the ID of the default camera
        CameraInfo cameraInfo = new CameraInfo();
        for(int i = 0; i < this.numberOfCameras; i++) {
            Camera.getCameraInfo(i, cameraInfo);
            if(cameraInfo.facing == CameraInfo.CAMERA_FACING_BACK) {
                this.defaultCameraId = i;
            }
        }
    }

    @Override
    protected void onResume() {Log.e(TAG, "onResume");
        super.onResume();

        //Open the default i.e. the first rear facing camera.
        this.mCamera = Camera.open();
        this.cameraCurrentlyLocked = this.defaultCameraId;
        this.mPreview.setCamera(mCamera);
    }

    @Override
    protected void onPause() {Log.e(TAG, "onPause");
        super.onPause();

        //Because the Camera object is a shared resource, it's very
        //Important to release it when the activity is paused.
        this.mPreview.releaseCamera();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //Inflate our menu which can gather user input for switching camera
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.camera_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        //Handle item selection
        switch (item.getItemId()) {
            case R.id.switchCam:
                //Check for availability of multiple cameras
                if(this.numberOfCameras == 1) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
                    builder.setMessage(this.getString(R.string.camera_alert)).setNeutralButton("Close", null);
                    AlertDialog alert = builder.create();
                    alert.show();
                    return true;
                }

                //OK, we have multiple cameras.
                //Release this camera -> cameraCurrentlyLocked
                this.mPreview.releaseCamera();

                //Acquire the next camera and request Preview to reconfigure parameters.
                this.mCamera = Camera.open((this.cameraCurrentlyLocked + 1) % this.numberOfCameras);
                this.cameraCurrentlyLocked = (this.cameraCurrentlyLocked + 1) % this.numberOfCameras;
                this.mPreview.switchCamera(mCamera);

                //Start the preview
                this.mCamera.startPreview();
                return true;

            case R.id.takePicture:
                this.mCamera.takePicture(null, null, jpegCallback);
                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }

    public void doPictureResults(byte[] data) {
        this.mPreview.releaseCamera();

        //Release the camera and send the results of the image to the GetResults view
        Intent resultsIntent = new Intent(MainActivity.this, ImageProcessorActivity.class);
        resultsIntent.putExtra("image_data", data);
        startActivity(resultsIntent);
    }

    /**
     * Handles data for jpeg picture when the picture is taken
     */
    PictureCallback jpegCallback = new PictureCallback() { 
        public void onPictureTaken(byte[] data, Camera mCamera) {Log.e(TAG, "jpegCallback");
            String baseExternalDir = Environment.getExternalStorageDirectory().getAbsolutePath();
            String fileName = String.format("Assist/%d.jpg", System.currentTimeMillis());

            FileOutputStream outStream = null;
            try {
                //Create the directory if needed
                File assistDirectory = new File(baseExternalDir + File.separator + "Assist");
                assistDirectory.mkdirs();

                // Write to SD Card
                outStream = new FileOutputStream(baseExternalDir + File.separator + fileName);
                outStream.write(data);
                outStream.close();
            } 
            catch (FileNotFoundException e) { 
                Log.e(TAG, "IOException caused by PictureCallback()", e);
            } 
            catch (IOException e) {
                Log.e(TAG, "IOException caused by PictureCallback()", e);
            } 

            //This is the type of thing I WANT to do....but its not possible.
            MainActivity.doPictureResults();
        }
    };
}
  • 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-05-27T09:52:08+00:00Added an answer on May 27, 2026 at 9:52 am

    One options would be to create a PictureCallback implementation that saved the information was required in doPictureResults. It’s not clear if doPictureResults will be called anywhere else; if it’s not, this is clean and isolates the functionality.

    Another would be to have the activity itself implement PictureCallback so you have direct access to all the member variables without having to do any work at all. This allows doPictureResults to be called from other places.

    public class MainActivity extends Activity implements PictureCallback {
        ...
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
                ....
                case R.id.takePicture:
                    this.mCamera.takePicture(null, null, this);
                    return true;
    
                default:
                    return super.onOptionsItemSelected(item);
            }
        }
        ...
        public void onPictureTaken(byte[] data, Camera mCamera) {
            Log.d(TAG, "jpegCallback");
            String baseExternalDir = Environment.getExternalStorageDirectory().getAbsolutePath();
            String fileName = String.format("%d.jpg", System.currentTimeMillis());
            ...
            doPictureResults();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application that has a feature of taking a picture. The image
I have an application that is taking UTF8 encoded characters and needs to send
I have a application that runs very well on JBoss using JSF 2. I
I have a weird bug in my application that causes an activity to relaunch
We have an application that creates several threads using the pthread library. As we
I have this iPhone application that let users take a picture and save it
I have a Java application that parses files and stores the data I need
I have an application that takes a picture and stores the picture internally in
I have an Android application in mind that would require taking as many camera
We have a web application that has been built using MySQL / PHP /

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.