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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:02:27+00:00 2026-05-16T15:02:27+00:00

In my app I’m capturing an image. Everything is working fine unless the phone

  • 0

In my app I’m capturing an image. Everything is working fine unless the phone goes to sleep while the preview is running. Not really sure how to handle it, I’m thinking that it may be best just to prevent the phone from automatically going to sleep while this process is in action. Which i do not know how to do. and if i do that will it prevent the phone from sleeping when i press the power button? perhaps there is a deeper issue here.

LogCat:


08-28 16:17:10.879: WARN/dalvikvm(9652): threadid=3: thread exiting with uncaught exception (group=0x4001b390)
08-28 16:17:10.879: ERROR/AndroidRuntime(9652): Uncaught handler: thread main exiting due to uncaught exception
08-28 16:17:10.879: ERROR/AndroidRuntime(9652): java.lang.RuntimeException: Method called after release()
08-28 16:17:10.879: ERROR/AndroidRuntime(9652):     at android.hardware.Camera.setHasPreviewCallback(Native Method)
08-28 16:17:10.879: ERROR/AndroidRuntime(9652):     at android.hardware.Camera.access$600(Camera.java:58)
08-28 16:17:10.879: ERROR/AndroidRuntime(9652):     at android.hardware.Camera$EventHandler.handleMessage(Camera.java:339)
08-28 16:17:10.879: ERROR/AndroidRuntime(9652):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-28 16:17:10.879: ERROR/AndroidRuntime(9652):     at android.os.Looper.loop(Looper.java:123)
08-28 16:17:10.879: ERROR/AndroidRuntime(9652):     at android.app.ActivityThread.main(ActivityThread.java:4595)
08-28 16:17:10.879: ERROR/AndroidRuntime(9652):     at java.lang.reflect.Method.invokeNative(Native Method)
08-28 16:17:10.879: ERROR/AndroidRuntime(9652):     at java.lang.reflect.Method.invoke(Method.java:521)
08-28 16:17:10.879: ERROR/AndroidRuntime(9652):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
08-28 16:17:10.879: ERROR/AndroidRuntime(9652):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
08-28 16:17:10.879: ERROR/AndroidRuntime(9652):     at dalvik.system.NativeStart.main(Native Method)

takephoto activity:

public class takephoto extends Activity {
     private static final String TAG = "GrowJournalDemo";
      Preview preview; // <1>
      Button buttonClick; // <2>
      String journ_id;
      String plant_id;
    // private String plantid = ((resource) this.getApplication()).getplantId();
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.photo);

        preview = new Preview(this); // <3>
        ((FrameLayout) findViewById(R.id.preview)).addView(preview); // <4>
        String journalid = ((resource) this.getApplication()).getjournalName();
        plant_id = ((resource) this.getApplication()).getplantId();
        journ_id = journalid;
        buttonClick = (Button) findViewById(R.id.buttonClick);
        buttonClick.setOnClickListener(new OnClickListener() {
          public void onClick(View v) { // <5>

             // preview.camera.takePicture(shutterCallback, rawCallback, jpegCallback);
              preview.camera.autoFocus(new Camera.AutoFocusCallback() {
                  Camera.ShutterCallback shutterCallback = new Camera.ShutterCallback() {
                    public void onShutter() {
                      // Play your sound here.
                    }
                  };
                  public void onAutoFocus(boolean success, Camera camera) {
                    preview.camera.takePicture(null, null, jpegCallback);
                  }
                });

          }
        });

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

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

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

      // Handles data for jpeg picture
      PictureCallback jpegCallback = new PictureCallback() { // <8>
        public void onPictureTaken(byte[] data, Camera camera) {
          FileOutputStream outStream = null;
          try {
              android.os.Environment.getExternalStorageState();
            // create a File object for the parent directory
              File PhotoDirectory = new File(
                      android.os.Environment.getExternalStorageDirectory()+
                      "/GrowJournalPhotos/"+journ_id+"/"+plant_id+"/");
              // have the object build the directory structure, if needed.
              PhotoDirectory.mkdirs();
              // create a File object for the output file
              File outputFile = new File(PhotoDirectory, "photo.jpg");
              // now attach the OutputStream to the file object, instead of a String representation
              outStream = new FileOutputStream(outputFile);

            outStream.write(data);
            outStream.close();
            Log.d(TAG, "onPictureTaken - wrote bytes: " + data.length);
            setResult(RESULT_OK);
            finish();

          } catch (FileNotFoundException e) { // <10>
            e.printStackTrace();
          } catch (IOException e) {
            e.printStackTrace();
          } finally {
          }
          Log.d(TAG, "onPictureTaken - jpeg");
        }
      };

    }

Preview surfaceview:

public 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.setPreviewDisplay(holder);  // <9>

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

      // Called when the holder is destroyed
      public void surfaceDestroyed(SurfaceHolder holder) {  // <14>
        camera.stopPreview();
        camera.release();
        camera = null;
      }

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


    }

I’ve added a wake lock to prevent the phone from dimming, I feel this is a temporary solution though as there is still a force close when the device is forced to sleep via power button.

  • 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-16T15:02:27+00:00Added an answer on May 16, 2026 at 3:02 pm

    Why are you calling invalidate() for every preview frame? The camera display should update without it.

    Either remove the preview callback altogether (since you don’t seem to be doing anything else anyway), or try setting camera.setPreviewCallback(null) in onResume() before you release so that it doesn’t try to call it after you have freed the cameras resources. You are getting an exception because you are trying to access the camera somewhere after you have released it.

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

Sidebar

Related Questions

my app was working perfectly fine, until I cleaned the targets and deleted the
My app was building and running fine on the simulator and device, but I
My app (winforms .net 2.0 / vs2008) works fine on my dev machine but
My app seems to crash sometimes when I rotate the phone. ViewControllerA has a
App crashes after execution of cellForRowAtIndexPath method of tableView It goes into: UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:withIndexPath:]
My app is working on local network,and the admin create the folder for every
My app has a login screen before a user goes into the main view.
The app works perfectly fine on the simulator and on the device. After adding
My app has an alarm which triggers after a period during which the phone
My app has many controls on its surface, and more are added dynamically at

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.