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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:28:42+00:00 2026-06-14T11:28:42+00:00

Im wondering if anyone could help me please, im new to android and am

  • 0

Im wondering if anyone could help me please, im new to android and am trying to write a small app which shows a camera preview from the front camera, all works good BUT when i close the app the phone freezes then crashes, ive been looking for an answer to this but cant seem to find one anywhere, code follows

import android.app.Activity;
import android.hardware.Camera;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

public class PreviewDemo extends Activity {

  private SurfaceView preview=null;
  private SurfaceHolder previewHolder=null;
  private Camera camera=null;
  private boolean inPreview=false;
  private boolean cameraConfigured=false;   
  public ImageView imageview1;
  public MediaPlayer mp;
  public MediaPlayer mp2;
  public MyCountDownTimer counter = new MyCountDownTimer(6000,1000);
  public MyCountDownTimer2 counter2 = new MyCountDownTimer2(2000,1000);


  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mp = MediaPlayer.create(PreviewDemo.this, R.raw.shatter);
    mp2 = MediaPlayer.create(PreviewDemo.this, R.raw.beep);
    preview=(SurfaceView)findViewById(R.id.preview);
    previewHolder=preview.getHolder();
    previewHolder.addCallback(surfaceCallback);
    previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    imageview1 = (ImageView) this.findViewById(R.id.imageView1);
    counter2.start();

  }

  @Override
  public void onResume() {
    super.onResume();
    int cameraCount = 0;
    Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
    cameraCount = Camera.getNumberOfCameras();
            for ( int camIdx = 0; camIdx < cameraCount; camIdx++ ) {
        Camera.getCameraInfo( camIdx, cameraInfo );
        if ( cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT  ) {
            try {
                camera = Camera.open( camIdx );
                camera.setDisplayOrientation(90);
            } catch (RuntimeException e) {
                ;
            }
        }
    startPreview();
  }
  }
  @Override
  public void onPause() {
    if (inPreview) {
      camera.stopPreview();
    }

    camera.release();
    camera=null;
    inPreview=false;

    super.onPause();
  }
  public void onFinish(){
  }

  private Camera.Size getBestPreviewSize(int width, int height,
                                         Camera.Parameters parameters) {
    Camera.Size result=null;

    for (Camera.Size size : parameters.getSupportedPreviewSizes()) {
      if (size.width<=width && size.height<=height) {
        if (result==null) {
          result=size;
        }
        else {
          int resultArea=result.width*result.height;
          int newArea=size.width*size.height;

          if (newArea>resultArea) {
            result=size;
          }
        }
      }
    }

    return(result);
  }

  private void initPreview(int width, int height) {
    if (camera!=null && previewHolder.getSurface()!=null) {
      try {
        camera.setPreviewDisplay(previewHolder);
      }
      catch (Throwable t) {
        Log.e("PreviewDemo-surfaceCallback",
              "Exception in setPreviewDisplay()", t);
        Toast
          .makeText(PreviewDemo.this, t.getMessage(), Toast.LENGTH_LONG)
          .show();
      }

      if (!cameraConfigured) {
        Camera.Parameters parameters=camera.getParameters();
        Camera.Size size=getBestPreviewSize(width, height,
                                            parameters);

        if (size!=null) {
          parameters.setPreviewSize(size.width, size.height);
          camera.setParameters(parameters);
          cameraConfigured=true;
        }
      }
    }
  }

  private void startPreview() {
    if (cameraConfigured && camera!=null) {
      camera.startPreview();
      inPreview=true;
    }
  }

  SurfaceHolder.Callback surfaceCallback=new SurfaceHolder.Callback() {
    public void surfaceCreated(SurfaceHolder holder) {
      // no-op -- wait until surfaceChanged()
    }

    public void surfaceChanged(SurfaceHolder holder,
                               int format, int width,
                               int height) {
      initPreview(width, height);
      startPreview();
    }

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

    }
  };
  public class MyCountDownTimer extends CountDownTimer{


      public MyCountDownTimer(long millisInFuture, long countDownInterval) {
      super(6000, 1000);
      }

      @Override
      public void onFinish() {
          imageview1.setVisibility(View.VISIBLE); 
          mp.start();
      }

      public void onTick(long millisUntilFinished) {
          mp2.start();
        }

      }

  public class MyCountDownTimer2 extends CountDownTimer{


      public MyCountDownTimer2(long millisInFuture, long countDownInterval) {
      super(2000, 1000);
      }

      @Override
      public void onFinish() {
          counter.start();
      }

      public void onTick(long millisUntilFinished) {

        }

      }
}

Cheers in advance
Mark

  • 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-14T11:28:43+00:00Added an answer on June 14, 2026 at 11:28 am

    You need to check if camera is valid, since you set camera = null in multiple places like onPause() and your SurfaceView onSurfaceDestroyed() callback. Your onPause() method should look like this:

    @Override
    public void onPause() {
        if(camera != null) {
            if (inPreview) {
              camera.stopPreview();
            }
    
            camera.release();
            camera=null;
        }
        inPreview=false;
    
        super.onPause();
    }
    

    This same logic is true for onSurfaceDestroyed(). Also your code might be easier to maintain if you move this logic into a closeCamera() method and call this method from valid locations.

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

Sidebar

Related Questions

im wondering if anyone could please help me with a css / html issue.
I was wondering if anyone could help me. I'm trying to use the PHP
Just wondering if anyone could help me with this. I'm new to actionscript, and
I am wondering if anyone could please help to draw a triangular grid (equilateral)
Wondering if anyone could help me? I've recently switched over to Mac and trying
Was wondering if anyone could help me on background threading on Android. I have
I am new to Xcode and was wondering if anyone could help me with
Hi I'm very very new at MySQL and was wondering if anyone could help
I'm pretty new to lisp; I was wondering if anyone here could help me
I was wondering if anyone could help me implement color selection in my iOS

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.