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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:08:34+00:00 2026-05-24T19:08:34+00:00

Every time I exit/pause my camera activity, my application crashes. I think it may

  • 0

Every time I exit/pause my camera activity, my application crashes. I think it may have something to do with releasing the camera on pause/exit, but I’m not sure where exactly to do it. An explanation would be much appreciated. Thanks!


CameraActivity.java


public class CameraActivity extends Activity {
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() {
        @Override
        public void onClick(View v) {
            preview.camera.takePicture(shutterCallback, rawCallback,
                    jpegCallback);
        }
    });
}

ShutterCallback shutterCallback = new ShutterCallback() {
    @Override
    public void onShutter() {
    }
};

PictureCallback rawCallback = new PictureCallback() {
    @Override
    public void onPictureTaken(byte[] data, Camera camera) {
    }
};

PictureCallback jpegCallback = new PictureCallback() { // <8>
    @Override
    public void onPictureTaken(byte[] data, Camera camera) {
                    doSomeStuff();                        
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            camera.stopPreview();
            camera.startPreview();
        }
    }
};
}

Preview.java


public class Preview extends SurfaceView implements SurfaceHolder.Callback { 
SurfaceHolder mHolder;
public Camera camera; 

Preview(Context context) {
    super(context);

    mHolder = getHolder(); 
    mHolder.addCallback(this); 
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

public void surfaceCreated(SurfaceHolder holder) {
    camera = Camera.open(); 
    camera.setDisplayOrientation(90);

    try {

        camera.setPreviewDisplay(holder); 
        camera.setPreviewCallback(new PreviewCallback() { 
            public void onPreviewFrame(byte[] data, Camera camera) { 
                Preview.this.invalidate(); 
            }
        });
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public void surfaceDestroyed(SurfaceHolder holder) { 
    camera.stopPreview();
    camera.release();
    camera = null;
}

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
    camera.startPreview();
}

}

edit: here’s the exception

08-18 01:18:50.242: ERROR/AndroidRuntime(10222): FATAL EXCEPTION: main
08-18 01:18:50.242: ERROR/AndroidRuntime(10222): java.lang.RuntimeException: Method called after release()
08-18 01:18:50.242: ERROR/AndroidRuntime(10222):     at android.hardware.Camera.setHasPreviewCallback(Native Method)
08-18 01:18:50.242: ERROR/AndroidRuntime(10222):     at android.hardware.Camera.access$600(Camera.java:114)
08-18 01:18:50.242: ERROR/AndroidRuntime(10222):     at android.hardware.Camera$EventHandler.handleMessage(Camera.java:545)
08-18 01:18:50.242: ERROR/AndroidRuntime(10222):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-18 01:18:50.242: ERROR/AndroidRuntime(10222):     at android.os.Looper.loop(Looper.java:143)
08-18 01:18:50.242: ERROR/AndroidRuntime(10222):     at android.app.ActivityThread.main(ActivityThread.java:4196)
08-18 01:18:50.242: ERROR/AndroidRuntime(10222):     at java.lang.reflect.Method.invokeNative(Native Method)
08-18 01:18:50.242: ERROR/AndroidRuntime(10222):     at java.lang.reflect.Method.invoke(Method.java:507)
08-18 01:18:50.242: ERROR/AndroidRuntime(10222):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-18 01:18:50.242: ERROR/AndroidRuntime(10222):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-18 01:18:50.242: ERROR/AndroidRuntime(10222):     at dalvik.system.NativeStart.main(Native Method)
  • 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-24T19:08:36+00:00Added an answer on May 24, 2026 at 7:08 pm

    Edit: Sorry, I take back the previous solution.
    The problem is with the onPreviewFrame callback, why do you need it? Once you set the preview display you don’t need to invalidate the view, it is done automatically by the camera.

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

Sidebar

Related Questions

Every time I turn on my company-owned development machine, I have to kill 10+
Every time I have to estimate time for a project (or review someone else's
Every time a user posts something containing < or > in a page in
Every time I try to run a small application that uses a Derby DB
Hello everyone I would have a value for the need to save every time
It's really irritating that every time I type exit() , I get prompted with
When I exit the interactive R shell, it displays an annoying prompt every time:
I have implemented Tab in my Application. Now I want to exit application with
I need my program to catch TimeOutException every time SerialPort Read Times out, but
I have a GUI I am trying to add a menubar to but every

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.