title: App Crashes On Completion Of Video Recording
body: <p>I used an online tutorial to capture and save images and videos respectively. On completion of either event, a preview is to show in either a videoview or imageview.
  The application uses intents to launch the inbuilt camera,and generates a unique name based on the timestamp.</p>
  
  <p>The images are shown on the imageview, but the application crashes whenever i stop recording a video.</p>
  
  <p>However the images and videos are both stored in the directory, and both use the same filenaming formats.</p>
  
  <p>No errors appear in the log cat.</p>
  
  <p>Below are code excerpts..</p>
  
  <pre><code>     //to start recording
       btnRecordVideo.setOnClickListener(new View.OnClickListener() {
  
          @Override
          public void onClick(View v) {
              // record video
              recordVideo();
          }
      });
  
           /**
   * Here we store the file url as it will be null after returning from camera
   * app
   */
  @Override
  protected void onSaveInstanceState(Bundle outState) {
      super.onSaveInstanceState(outState);
  
      // save file url in bundle as it will be null on scren orientation
      // changes
      outState.putParcelable("file_uri", fileUri);
  }
  
  @Override
  protected void onRestoreInstanceState(Bundle savedInstanceState) {
      super.onRestoreInstanceState(savedInstanceState);
  
      // get the file url
      fileUri = savedInstanceState.getParcelable("file_uri");
  }
  
  /**
   * Recording video
   */
  private void recordVideo() {
      Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
  
      fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO);
  
      // set video quality
      intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
  
      intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file
                                                          // name
  
      // start the video capture Intent
      startActivityForResult(intent, CAMERA_CAPTURE_VIDEO_REQUEST_CODE);
  }
  
  /**
   * Receiving activity result method will be called after closing the camera
   * */
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      // if the result is capturing Image
      if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {
          if (resultCode == RESULT_OK) {
              // successfully captured the image
              // display it in image view
              previewCapturedImage();
          } else if (resultCode == RESULT_CANCELED) {
              // user cancelled Image capture
              Toast.makeText(getApplicationContext(),
                      "User cancelled image capture", Toast.LENGTH_SHORT)
                      .show();
          } else {
              // failed to capture image
              Toast.makeText(getApplicationContext(),
                      "Sorry! Failed to capture image", Toast.LENGTH_SHORT)
                      .show();
          }
      } else if (requestCode == CAMERA_CAPTURE_VIDEO_REQUEST_CODE) {
          if (resultCode == RESULT_OK) {
              // video successfully recorded
              // preview the recorded video
              previewVideo();
          } else if (resultCode == RESULT_CANCELED) {
              // user cancelled recording
              Toast.makeText(getApplicationContext(),
                      "User cancelled video recording", Toast.LENGTH_SHORT)
                      .show();
          } else {
              // failed to record video
              Toast.makeText(getApplicationContext(),
                      "Sorry! Failed to record video", Toast.LENGTH_SHORT)
                      .show();
          }
      }
  }
  
  /**
  
  
          /**
   * Previewing recorded video
   */
  private void previewVideo() {
      try {
          // hide image preview
          imgPreview.setVisibility(View.GONE);
  
          videoPreview.setVisibility(View.VISIBLE);
          //videoPreview.setVideoURI(fileUri);
          videoPreview.setVideoPath(fileUri.getPath());
          // start playing
          videoPreview.start();
      } catch (Exception e) {
          e.printStackTrace();
      }
  }
  
  /**
   * ------------ Helper Methods ---------------------- 
   * */
  
  /**
   * Creating file uri to store image/video
   */
  public Uri getOutputMediaFileUri(int type) {
      return Uri.fromFile(getOutputMediaFile(type));
  }
  
  /**
   * returning image / video
   */
  private static File getOutputMediaFile(int type) {
  
      // External sdcard location
      File mediaStorageDir = new File(
              Environment
                      .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
              IMAGE_DIRECTORY_NAME);
  
      // Create the storage directory if it does not exist
      if (!mediaStorageDir.exists()) {
          if (!mediaStorageDir.mkdirs()) {
              Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create "
                      + IMAGE_DIRECTORY_NAME + " directory");
              return null;
          }
      }
  
      // Create a media file name
      String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
              Locale.getDefault()).format(new Date());
      String mediaFile2;
      File mediaFile;
      if (type == MEDIA_TYPE_IMAGE) {
          mediaFile = new File(mediaStorageDir.getPath() + File.separator
                  + "IMG_" + timeStamp + ".jpg");
      } else if (type == MEDIA_TYPE_VIDEO) {
          mediaFile = new File(mediaStorageDir.getPath() + File.separator
                  + "VID_" + timeStamp + ".mp4");
      } else {
          return null;
      }
          return mediaFile;
  }
  </code></pre>
  
  <p>As stated earlier....no errors are displayed in logcat.
  P.S testing on Samsung Galaxy ACE running Froyo (2.2.1)</p>