private void returnResult() {
   File sourceMedia =
       (uriFragmentToMedia != null)
           ? ODKFileUtils.getRowpathFile(appName, tableId, instanceId, uriFragmentToMedia)
           : null;
   if (sourceMedia != null && sourceMedia.exists()) {
     Intent i = new Intent();
     i.putExtra(
         IntentConsts.INTENT_KEY_URI_FRAGMENT,
         ODKFileUtils.asRowpathUri(appName, tableId, instanceId, sourceMedia));
     String name = sourceMedia.getName();
     i.putExtra(
         IntentConsts.INTENT_KEY_CONTENT_TYPE,
         MEDIA_CLASS + name.substring(name.lastIndexOf(".") + 1));
     setResult(Activity.RESULT_OK, i);
     finish();
   } else {
     WebLogger.getLogger(appName)
         .e(
             t,
             ERROR_NO_FILE
                 + ((uriFragmentToMedia != null)
                     ? sourceMedia.getAbsolutePath()
                     : "null mediaPath"));
     Toast.makeText(this, R.string.media_save_failed, Toast.LENGTH_SHORT).show();
     setResult(Activity.RESULT_CANCELED);
     finish();
   }
 }
 private void deleteMedia() {
   if (uriFragmentToMedia == null) {
     return;
   }
   // get the file path and delete the file
   File f = ODKFileUtils.getRowpathFile(appName, tableId, instanceId, uriFragmentToMedia);
   String path = f.getAbsolutePath();
   // delete from media provider
   int del = MediaUtils.deleteVideoFileFromMediaProvider(this, appName, path);
   WebLogger.getLogger(appName).i(t, "Deleted " + del + " rows from video media content provider");
 }
  @Override
  protected void onResume() {
    super.onResume();

    if (afterResult) {
      // this occurs if we re-orient the phone during the save-recording
      // action
      returnResult();
    } else if (!hasLaunched && !afterResult) {
      Intent i = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
      // to make the name unique...
      File f =
          ODKFileUtils.getRowpathFile(
              appName,
              tableId,
              instanceId,
              (uriFragmentToMedia == null ? uriFragmentNewFileBase : uriFragmentToMedia));
      int idx = f.getName().lastIndexOf('.');
      if (idx == -1) {
        i.putExtra(Video.Media.DISPLAY_NAME, f.getName());
      } else {
        i.putExtra(Video.Media.DISPLAY_NAME, f.getName().substring(0, idx));
      }

      // Need to have this ugly code to account for
      // a bug in the Nexus 7 on 4.3 not returning the mediaUri in the data
      // of the intent - using the MediaStore.EXTRA_OUTPUT to get the data
      // Have it saving to an intermediate location instead of final destination
      // to allow the current location to catch issues with the intermediate file
      WebLogger.getLogger(appName).i(t, "The build of this device is " + android.os.Build.MODEL);
      if (NEXUS7.equals(android.os.Build.MODEL) && Build.VERSION.SDK_INT == 18) {
        nexus7Uri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO);
        i.putExtra(MediaStore.EXTRA_OUTPUT, nexus7Uri);
      }

      try {
        hasLaunched = true;
        startActivityForResult(i, ACTION_CODE);
      } catch (ActivityNotFoundException e) {
        String err = getString(R.string.activity_not_found, MediaStore.ACTION_VIDEO_CAPTURE);
        WebLogger.getLogger(appName).e(t, err);
        Toast.makeText(this, err, Toast.LENGTH_SHORT).show();
        setResult(Activity.RESULT_CANCELED);
        finish();
      }
    }
  }
  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent intent) {

    if (resultCode == Activity.RESULT_CANCELED) {
      // request was canceled -- propagate
      setResult(Activity.RESULT_CANCELED);
      finish();
      return;
    }

    Uri mediaUri = intent.getData();

    // it is unclear whether getData() always returns a value or if
    // getDataString() does...
    String str = intent.getDataString();
    if (mediaUri == null && str != null) {
      WebLogger.getLogger(appName).w(t, "Attempting to work around null mediaUri");
      mediaUri = Uri.parse(str);
    }

    if (mediaUri == null) {
      // we are in trouble
      WebLogger.getLogger(appName).e(t, "No uri returned from ACTION_CAPTURE_VIDEO!");
      setResult(Activity.RESULT_CANCELED);
      finish();
      return;
    }

    // Remove the current media.
    deleteMedia();

    // get the file path and create a copy in the instance folder
    String binaryPath = MediaUtils.getPathFromUri(this, (Uri) mediaUri, Video.Media.DATA);
    File source = new File(binaryPath);
    String extension = binaryPath.substring(binaryPath.lastIndexOf("."));

    if (uriFragmentToMedia == null) {
      // use the newFileBase as a starting point...
      uriFragmentToMedia = uriFragmentNewFileBase + extension;
    }

    // adjust the mediaPath (destination) to have the same extension
    // and delete any existing file.
    File f = ODKFileUtils.getRowpathFile(appName, tableId, instanceId, uriFragmentToMedia);
    File sourceMedia =
        new File(
            f.getParentFile(), f.getName().substring(0, f.getName().lastIndexOf('.')) + extension);
    uriFragmentToMedia = ODKFileUtils.asRowpathUri(appName, tableId, instanceId, sourceMedia);
    deleteMedia();

    try {
      FileUtils.copyFile(source, sourceMedia);
    } catch (IOException e) {
      WebLogger.getLogger(appName).e(t, ERROR_COPY_FILE + sourceMedia.getAbsolutePath());
      Toast.makeText(this, R.string.media_save_failed, Toast.LENGTH_SHORT).show();
      deleteMedia();
      setResult(Activity.RESULT_CANCELED);
      finish();
      return;
    }

    if (sourceMedia.exists()) {
      // Add the copy to the content provier
      ContentValues values = new ContentValues(6);
      values.put(Video.Media.TITLE, sourceMedia.getName());
      values.put(Video.Media.DISPLAY_NAME, sourceMedia.getName());
      values.put(Video.Media.DATE_ADDED, System.currentTimeMillis());
      values.put(Video.Media.DATA, sourceMedia.getAbsolutePath());

      Uri MediaURI =
          getApplicationContext()
              .getContentResolver()
              .insert(Video.Media.EXTERNAL_CONTENT_URI, values);
      WebLogger.getLogger(appName).i(t, "Inserting VIDEO returned uri = " + MediaURI.toString());
      uriFragmentToMedia = ODKFileUtils.asRowpathUri(appName, tableId, instanceId, sourceMedia);
      WebLogger.getLogger(appName)
          .i(t, "Setting current answer to " + sourceMedia.getAbsolutePath());

      // Need to have this ugly code to account for
      // a bug in the Nexus 7 on 4.3 not returning the mediaUri in the data
      // of the intent - uri in this case is a file
      int delCount = 0;
      if (NEXUS7.equals(android.os.Build.MODEL) && Build.VERSION.SDK_INT == 18) {
        File fileToDelete = new File(mediaUri.getPath());
        delCount = fileToDelete.delete() ? 1 : 0;
      } else {
        delCount = getApplicationContext().getContentResolver().delete(mediaUri, null, null);
      }
      WebLogger.getLogger(appName)
          .i(
              t,
              "Deleting original capture of file: " + mediaUri.toString() + " count: " + delCount);
    } else {
      WebLogger.getLogger(appName).e(t, "Inserting Video file FAILED");
    }

    /*
     * We saved the audio to the instance directory. Verify that it is there...
     */
    returnResult();
    return;
  }