Search This Blog

Support my cause by downloading

Sunday, 6 May 2012

Taking picture and Video from Camera and displaying or playing in android

Taking picture in android has two way to achieve it

1) Using  Surface view with Camera integration
2)Starting Default Camera Activity that will return an intent with picture

first way i have discussed in How to take picture with surface View and camera .Now i am going to discuss second way of taking picture from android camera. And In code you will also know how to take Video in android and Playing it. I will display taken image in ImageView and video will be start paying in a videoview. User Interface will be similar to this when you will run code

Source code and screen shot are available at the end of article

private void dispatchTakePictureIntent(int actionCode) {

        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            File f = null;
            try {
                f = setUpPhotoFile();
                mCurrentPhotoPath = f.getAbsolutePath();
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
            } catch (IOException e) {
                f = null;
                mCurrentPhotoPath = null;
       } // switch
startActivityForResult(takePictureIntent, actionCode);

This method will dispatch a intent and will start an Camera Activity . In my code i have two option weather you want to take big picture or small one (to avoid memory error ). So you will get a switch case there. actioncode is decide, we will have to handle big Image or small Image.

Here are the step to create file path to save image

setUpPhotoFile() this method is used to create a file for saving image

 mCurrentPhotoPath = f.getAbsolutePath(); // this will save the file path and will pass into intent so that it will take it to save image later on

finally we will start activity

startActivityForResult(takePictureIntent, actionCode);

Now if we click picture then we have to handle intent data inside the onActivityResult()

private void handleSmallCameraPhoto(Intent intent) {
  Bundle extras = intent.getExtras();
  mImageBitmap = (Bitmap) extras.get("data");
  mVideoUri = null;

In activity result i have three method  one for handle small picture intent, second to handle big picture then one for handle Video .If you are taking video or big picture then study these method

 private void handleBigCameraPhoto() {

  if (mCurrentPhotoPath != null) {
   mCurrentPhotoPath = null;

      private void handleCameraVideo(Intent intent) {
  mVideoUri = intent.getData();
  mImageBitmap = null;

                                                        Screen shot of this project


Source Code

Android News and source code