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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:09:53+00:00 2026-06-18T04:09:53+00:00

I do not have much experience with the Camera functionality in Android. I need

  • 0

I do not have much experience with the Camera functionality in Android. I need to take a photo through code which I am planning to send back to a server. I based my logic off of this post and it works well as long as I do not add mCamera.takePicture(null, null, mPictureCallback); to the end of the surfaceChange method (which is what I want I guess). When I do add that line, it works sometimes but most of the times, I just get back a black screen. This is the main problem. I don’t think there is a compatibility issue with the device because I got this functionality working atleast 3-4 times earlier.

My device that I am testing on is Galaxy Nexus

public class CameraView extends Activity implements SurfaceHolder.Callback, OnClickListener
{
    private static final String TAG = "CameraTest";
    Camera mCamera;
    boolean mPreviewRunning = false; 

    public void onCreate(Bundle icicle) 
    {
        super.onCreate(icicle);
        Log.e(TAG, "onCreate");

        getWindow().setFormat(PixelFormat.TRANSLUCENT);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_camera_view);
        mSurfaceView = (SurfaceView) findViewById(R.id.surface_camera);
        mSurfaceView.setOnClickListener(this);
        mSurfaceHolder = mSurfaceView.getHolder();
        mSurfaceHolder.addCallback(this);
        mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) 
    {
        super.onRestoreInstanceState(savedInstanceState);
    }


    Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {

        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            // TODO Auto-generated method stub
            if (data != null) 
            {
                //Intent mIntent = new Intent();
                //mIntent.putExtra("image",imageData);

                mCamera.stopPreview();
                mPreviewRunning = false;
                mCamera.release();


                 try
                   {
                   BitmapFactory.Options opts = new BitmapFactory.Options();
                   Bitmap bitmap= BitmapFactory.decodeByteArray(data, 0, data.length,opts);
                   bitmap = Bitmap.createScaledBitmap(bitmap, 480, 480, false);
                   CameraProjectActivity.image.setImageBitmap(bitmap);
                   }
                   catch(Exception e)
                   {
                         e.printStackTrace();
                   }
                //StoreByteImage(mContext, imageData, 50,"ImageName");
                //setResult(FOTO_MODE, mIntent);
                setResult(585);
                finish();
            }       
        }
    };




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

    protected void onSaveInstanceState(Bundle outState) 
    {
        super.onSaveInstanceState(outState);
    }

    protected void onStop() 
    {
        Log.e(TAG, "onStop");
        super.onStop();
    }

    public void surfaceCreated(SurfaceHolder holder) 
    {
        Log.e(TAG, "surfaceCreated");
        mCamera = Camera.open();
        mCamera.setDisplayOrientation(90);

    }

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        Log.e(TAG, "surfaceChanged");

        // XXX stopPreview() will crash if preview is not running
        if (mPreviewRunning) 
        {
            mCamera.stopPreview();
        }

        Camera.Parameters p = mCamera.getParameters();

        List<Camera.Size> previewSizes = p.getSupportedPreviewSizes();

        Camera.Size previewSize = previewSizes.get(3);
        p.setPreviewSize(previewSize.width, previewSize.height);


        mCamera.setParameters(p);
        try 
        {
            mCamera.setPreviewDisplay(holder);
        } 
        catch (Exception e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        mCamera.startPreview();
        mPreviewRunning = true;

    // THIS IS THE CODE THAT BREAKS IT. IS THERE ANY OTHER WAY TO DO THIS??? ********
    // mCamera.takePicture(null, null, mPictureCallback);


    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        Log.e(TAG, "surfaceDestroyed");
//        mCamera.stopPreview();
//        mPreviewRunning = false;
//        mCamera.release();
    }

    private SurfaceView mSurfaceView;
    private SurfaceHolder mSurfaceHolder; 

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        mCamera.takePicture(null, mPictureCallback, mPictureCallback);
    }
}

Any help would be appreciated.
Thanks!

  • 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-18T04:09:54+00:00Added an answer on June 18, 2026 at 4:09 am

    I am not sure if this is a good solution, but I just fixed it by making the surfaceChanged() method synchronized and making the thread wait for a second just before I call takePicture.

    try {
        this.wait(1000);
        mCamera.takePicture(null, null, mPictureCallback);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I do not have much experience with writing macros, and therefore need the help
I am looking into someone else's code and do not have much experience with
i'm a beginner in android and have not much experience on java... I have
I'm currently extending a Site and do not have much experience with symfony. Currently
I have 4 years experience on DotNet, not much. I now learned developing for
I have a fair amount of JSP and JSF experience. Not so much with
I have a much simpler set of requirements and do not need much of
I do not have much experience coding systems dealing with web-services. Please help me
I have not had much experience with VBA, but I sometimes use it at
I'm a Drupal newbie plus I don't have much experience updating code, styles, and

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.