App freeze when trying to capture camara image

All your problems with Audio, Video and Images.

App freeze when trying to capture camara image

Postby ajoj11 » Tue Mar 15, 2011 9:17 pm

hi

I have a problem. I try to make a app that takes pictures using the device camera but every time i try to save the picture my Xperia freeze, i think i did something wrong in the code but i'm not sure:

Code: Select all
package com.crycam.campont;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.hardware.Camera.PictureCallback;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.Window;

public class CameraActivity extends Activity implements Callback {

   SurfaceView mSurfaceView;
   private Camera mCamera;
   
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      requestWindowFeature(Window.FEATURE_NO_TITLE);
      setContentView(R.layout.usecamera);
      mSurfaceView = (SurfaceView) findViewById(R.id.sv_Camera);
      mSurfaceView.getHolder().addCallback(this);
      mSurfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
   }

   @Override
   public void surfaceChanged(SurfaceHolder holder, int format, int width,
         int height) {
      Parameters parameter = mCamera.getParameters();
      parameter.setPreviewSize(width, height);
      parameter.setPictureFormat(PixelFormat.JPEG);
      mCamera.setParameters(parameter);
      mCamera.startPreview();
   }

   @Override
   public void surfaceCreated(SurfaceHolder holder) {
      mCamera = Camera.open();
      try {
         mCamera.setPreviewDisplay(holder);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }

   @Override
   public void surfaceDestroyed(SurfaceHolder holder) {
      mCamera.stopPreview();
      mCamera.release();
      mCamera = null;
   }

   @Override
   public boolean onTouchEvent(MotionEvent event) {
      PictureCallback pictureCallback =
         new PictureCallback() {
            
            @Override
            public void onPictureTaken(byte[] data, Camera camera) {
               try {
                  File bild = new File(
                        CameraActivity.this.getFilesDir() + "/bild.jpg");
                  FileOutputStream bildOut =
                     new FileOutputStream(bild);
                  bildOut.write(data);
                  bildOut.flush();
                  bildOut.close();
               } catch (Exception e) {
                  e.printStackTrace();
               }
               mCamera.startPreview();
            }
         };
      mCamera.takePicture(null, null, pictureCallback);
      return super.onTouchEvent(event);
   }
}


after my app time out's i can't use any other camera app it says the camera is used by an other process.

Plz help
ajoj11
Freshman
Freshman
 
Posts: 2
Joined: Tue Feb 22, 2011 6:48 pm

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 16 guests