Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
194 views
in Technique[技术] by (71.8m points)

android - How to capture the image and save it in sd card

hey guys i am using the following code to access camera from my application. The application is able to access the camera i have also added a button whose onclicklistener adds this line of code :-

camera.takePicture(mShutterCallback, mPictureCallbackRaw, mPictureCallbackJpeg);

Now i dont know what happens but the application gets stuck i have to force close it and then i can not even access the native camera application. I think it leaves the application without releasing the Camera object. Plus the image never gets saved.

Code :-

SurfaceHolder holder;
SurfaceView  surface;
Camera camera;
Boolean isPreviewRunning, fromOnResume;
//Preview mpreview;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    isPreviewRunning = false;
    fromOnResume = false;
   // requestWindowFeature(Window.FEATURE_NO_TITLE);
    surface = (SurfaceView)findViewById(R.id.surface);
    holder = surface.getHolder();
  holder.addCallback(this);
  holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

  Button btn = (Button)findViewById(R.id.click);
    btn.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            camera.takePicture(mShutterCallback, mPictureCallbackRaw, mPictureCallbackJpeg);
            //onCreate(null);


        }

    });

}


@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
/*  if (isPreviewRunning) {  
          camera.stopPreview();  
         }  */

    Camera.Parameters parameters = camera.getParameters();

    List<Size> sizes = parameters.getSupportedPreviewSizes();
    Size size = sizes.get(0);
    parameters.setPreviewSize(size.width, size.height);

    camera.setParameters(parameters);
     camera.startPreview();  
     isPreviewRunning=true;
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
    // TODO Auto-generated method stub

     camera = Camera.open();

     try {
        camera.setPreviewDisplay(holder);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();

    }
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    // TODO Auto-generated method stub

    camera.stopPreview();
    camera.release();
    camera=null;
}

 Camera.PictureCallback mPictureCallbackRaw = new Camera.PictureCallback() {
 public void onPictureTaken(byte[] data, Camera c) {
  Log.e(getClass().getSimpleName(), "PICTURE CALLBACK RAW: " + data);
  camera.startPreview();
 }
 };

 Camera.PictureCallback mPictureCallbackJpeg= new Camera.PictureCallback() {
 public void onPictureTaken(byte[] data, Camera c) {
  Log.e(getClass().getSimpleName(), "PICTURE CALLBACK JPEG: data.length = " + data);

  camera.startPreview();
 }
 };


 Camera.ShutterCallback mShutterCallback = new Camera.ShutterCallback() {
 public void onShutter() {
 Log.e(getClass().getSimpleName(), "SHUTTER CALLBACK");
 }
 };
/*
 *  protected void onRestoreInstanceState(Bundle savedInstanceState)
 {

 super.onRestoreInstanceState(savedInstanceState);
 }
 protected void onResume()
 {

 Log.e(getClass().getSimpleName(), "onResume");
 camera.open();
 fromOnResume=true;
 super.onResume();
 }

 protected void onSaveInstanceState(Bundle outState)
 {
 super.onSaveInstanceState(outState);
 }

 @Override
protected void onPause() {
    // TODO Auto-generated method stub
     camera.release();
    super.onPause();
}


 protected void onStop()
 {
 Log.e(getClass().getSimpleName(), "onStop");
 super.onStop();
 }*/
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I know this isn't exactly an answer to your question, but wouldn'nt it be easier to use the stock camera application? You can access it using this code in your activity:

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.capture);

    Button capture = (Button) findViewById(R.id.capture_button);
    capture.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            // We use the stock camera app to take a photo
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, getImageUri());
            startActivityForResult(intent, TAKE_PHOTO_CODE);
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) {
        Uri imagePath = getImageUri();

        doSomething();
    }
}

/**
 * Get the uri of the captured file
 * @return A Uri which path is the path of an image file, stored on the dcim folder
 */
private Uri getImageUri() {
    // Store image in dcim
    File file = new File(Environment.getExternalStorageDirectory() + "/DCIM", CAPTURE_TITLE);
    Uri imgUri = Uri.fromFile(file);

    return imgUri;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...