相关文章推荐
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Ok this has been happening to me a few times over the past few weeks and I can't figure out how to fix it. Basically my app uses the camera but every so often after a few goes with the camera on the app it then force closes sometimes and cannot be used even with other apps or the default camera on the phone.

I found the only way to fix this is to restart the phone.

From what I have read so far I understand I need to call

camera.release();

camera = null;

But I still get the error of force close sometimes, is it possible for someone just to take a look through maybe I am missing a camera.release(); somewhere.

public class AndroidCamera extends Activity implements SurfaceHolder.Callback{
Camera camera;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
boolean previewing = false;
LayoutInflater controlInflater = null;
final int RESULT_SAVEIMAGE = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    getWindow().setFormat(PixelFormat.UNKNOWN);
    surfaceView = (SurfaceView)findViewById(R.id.camerapreview);
    surfaceHolder = surfaceView.getHolder();
    surfaceHolder.addCallback(this);
    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    controlInflater = LayoutInflater.from(getBaseContext());
    View viewControl = controlInflater.inflate(R.layout.control, null);
    LayoutParams layoutParamsControl 
        = new LayoutParams(LayoutParams.FILL_PARENT, 
        LayoutParams.FILL_PARENT);
    this.addContentView(viewControl, layoutParamsControl);
    Button buttonTakePicture = (Button)findViewById(R.id.takepicture);
    buttonTakePicture.setOnClickListener(new Button.OnClickListener(){
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            camera.takePicture(myShutterCallback, 
                    myPictureCallback_RAW, myPictureCallback_JPG);
ShutterCallback myShutterCallback = new ShutterCallback(){
    public void onShutter() {
        // TODO Auto-generated method stub
PictureCallback myPictureCallback_RAW = new PictureCallback(){
    public void onPictureTaken(byte[] arg0, Camera arg1) {
        // TODO Auto-generated method stub
PictureCallback myPictureCallback_JPG = new PictureCallback(){
    public void onPictureTaken(byte[] arg0, Camera arg1) {
        // TODO Auto-generated method stub
        /*Bitmap bitmapPicture 
            = BitmapFactory.decodeByteArray(arg0, 0, arg0.length);  */
        int imageNum = 0;
        Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        File imagesFolder = new File(Environment.getExternalStorageDirectory(), "Punch");
        imagesFolder.mkdirs(); // <----
        String fileName = "image_" + String.valueOf(imageNum) + ".jpg";
        File output = new File(imagesFolder, fileName);
        while (output.exists()){
            imageNum++;
            fileName = "image_" + String.valueOf(imageNum) + ".jpg";
            output = new File(imagesFolder, fileName);
        Uri uriSavedImage = Uri.fromFile(output);
        imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
        OutputStream imageFileOS;
        try {
            imageFileOS = getContentResolver().openOutputStream(uriSavedImage);
            imageFileOS.write(arg0);
            imageFileOS.flush();
            imageFileOS.close();
            Toast.makeText(AndroidCamera.this, 
                    "Image saved", 
                    Toast.LENGTH_LONG).show();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        Intent intent = new Intent(getBaseContext(), Punch.class);
        intent.putExtra("filepath",uriSavedImage.toString());
        //just using a request code of zero
        int request=0;
        startActivityForResult(intent,request); 
public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
    // TODO Auto-generated method stub
    if(previewing){
        camera.stopPreview();
        previewing = false;
    if (camera != null){
        try {
            camera.setPreviewDisplay(surfaceHolder);
            camera.startPreview();
            previewing = true;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            camera.release();
            e.printStackTrace();
public void surfaceCreated(SurfaceHolder holder) {
    // TODO Auto-generated method stub
    camera = Camera.open();
    try {
           Camera.Parameters parameters = camera.getParameters();
           if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
              // This is an undocumented although widely known feature
              parameters.set("orientation", "portrait");
              // For Android 2.2 and above
              camera.setDisplayOrientation(90);
              // Uncomment for Android 2.0 and above
              parameters.setRotation(90);
           } else {
              // This is an undocumented although widely known feature
              parameters.set("orientation", "landscape");
              // For Android 2.2 and above
              camera.setDisplayOrientation(0);
              // Uncomment for Android 2.0 and above
              parameters.setRotation(0);
          camera.setParameters(parameters);
          camera.setPreviewDisplay(holder);
      } catch (IOException exception) {
         camera.release();
        camera.startPreview();
public void surfaceDestroyed(SurfaceHolder holder) {
    // TODO Auto-generated method stub
    if(previewing && camera != null) {
        if(camera!=null) {
            camera.stopPreview();
            camera.release();  
            camera = null;
        previewing = false;

The logcat gave me (there is more but I think its because the camera force closes on me so the rest of the app doesn't run.

01-03 14:59:17.835: D/AndroidRuntime(16531): Shutting down VM

01-03 14:59:17.835: W/dalvikvm(16531): threadid=1: thread exiting with uncaught exception (group=0x4001d5a0)

01-03 14:59:17.845: E/AndroidRuntime(16531): FATAL EXCEPTION: main

01-03 14:59:17.845: E/AndroidRuntime(16531): java.lang.RuntimeException: Fail to connect to camera service

i am having a very similar problem where an app that i am writing crashed before getting to release the camera, now nothing can use the camera without restarting the phone. – dylan murphy Jan 19, 2012 at 19:40

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

 
推荐文章