@Override
  public void setBinaryData(Object answer) {
    // you are replacing an answer. delete the previous image using the
    // content provider.
    if (mBinaryName != null) {
      deleteMedia();
    }

    File newImage = (File) answer;
    if (newImage.exists()) {
      // Add the new image to the Media content provider so that the
      // viewing is fast in Android 2.0+
      ContentValues values = new ContentValues(6);
      values.put(Images.Media.TITLE, newImage.getName());
      values.put(Images.Media.DISPLAY_NAME, newImage.getName());
      values.put(Images.Media.DATE_TAKEN, System.currentTimeMillis());
      values.put(Images.Media.MIME_TYPE, "image/jpeg");
      values.put(Images.Media.DATA, newImage.getAbsolutePath());

      Uri imageURI =
          getContext().getContentResolver().insert(Images.Media.EXTERNAL_CONTENT_URI, values);
      Log.i(t, "Inserting image returned uri = " + imageURI.toString());

      mBinaryName = newImage.getName();
      Log.i(t, "Setting current answer to " + newImage.getName());
    } else {
      Log.e(t, "NO IMAGE EXISTS at: " + newImage.getAbsolutePath());
    }

    Collect.getInstance().getFormController().setIndexWaitingForData(null);
  }
  private void launchSignatureActivity() {
    mErrorTextView.setVisibility(View.GONE);
    Intent i = new Intent(getContext(), DrawActivity.class);
    i.putExtra(DrawActivity.OPTION, DrawActivity.OPTION_SIGNATURE);
    // copy...
    if (mBinaryName != null) {
      File f = new File(mInstanceFolder + File.separator + mBinaryName);
      i.putExtra(DrawActivity.REF_IMAGE, Uri.fromFile(f));
    }
    i.putExtra(DrawActivity.EXTRA_OUTPUT, Uri.fromFile(new File(Collect.TMPFILE_PATH)));

    try {
      Collect.getInstance().getFormController().setIndexWaitingForData(mPrompt.getIndex());
      ((Activity) getContext()).startActivityForResult(i, FormEntryActivity.SIGNATURE_CAPTURE);
    } catch (ActivityNotFoundException e) {
      Toast.makeText(
              getContext(),
              getContext().getString(R.string.activity_not_found, "signature capture"),
              Toast.LENGTH_SHORT)
          .show();
      Collect.getInstance().getFormController().setIndexWaitingForData(null);
    }
  }
  public SignatureWidget(Context context, FormEntryPrompt prompt) {
    super(context, prompt);

    mInstanceFolder = Collect.getInstance().getFormController().getInstancePath().getParent();

    setOrientation(LinearLayout.VERTICAL);

    TableLayout.LayoutParams params = new TableLayout.LayoutParams();
    params.setMargins(7, 5, 7, 5);

    mErrorTextView = new TextView(context);
    mErrorTextView.setId(QuestionWidget.newUniqueId());
    mErrorTextView.setText("Selected file is not a valid image");

    // setup Blank Image Button
    mSignButton = new Button(getContext());
    mSignButton.setId(QuestionWidget.newUniqueId());
    mSignButton.setText(getContext().getString(R.string.sign_button));
    mSignButton.setTextSize(TypedValue.COMPLEX_UNIT_DIP, mAnswerFontsize);
    mSignButton.setPadding(20, 20, 20, 20);
    mSignButton.setEnabled(!prompt.isReadOnly());
    mSignButton.setLayoutParams(params);
    // launch capture intent on click
    mSignButton.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            Collect.getInstance()
                .getActivityLogger()
                .logInstanceAction(this, "signButton", "click", mPrompt.getIndex());
            launchSignatureActivity();
          }
        });

    // finish complex layout
    addView(mSignButton);
    addView(mErrorTextView);

    // and hide the sign button if read-only
    if (prompt.isReadOnly()) {
      mSignButton.setVisibility(View.GONE);
    }
    mErrorTextView.setVisibility(View.GONE);

    // retrieve answer from data model and update ui
    mBinaryName = prompt.getAnswerText();

    // Only add the imageView if the user has signed
    if (mBinaryName != null) {
      mImageView = new ImageView(getContext());
      mImageView.setId(QuestionWidget.newUniqueId());
      Display display =
          ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE))
              .getDefaultDisplay();
      int screenWidth = display.getWidth();
      int screenHeight = display.getHeight();

      File f = new File(mInstanceFolder + File.separator + mBinaryName);

      if (f.exists()) {
        Bitmap bmp = FileUtils.getBitmapScaledToDisplay(f, screenHeight, screenWidth);
        if (bmp == null) {
          mErrorTextView.setVisibility(View.VISIBLE);
        }
        mImageView.setImageBitmap(bmp);
      } else {
        mImageView.setImageBitmap(null);
      }

      mImageView.setPadding(10, 10, 10, 10);
      mImageView.setAdjustViewBounds(true);
      mImageView.setOnClickListener(
          new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              Collect.getInstance()
                  .getActivityLogger()
                  .logInstanceAction(this, "viewImage", "click", mPrompt.getIndex());
              launchSignatureActivity();
            }
          });

      addView(mImageView);
    }
  }
 @Override
 public void cancelWaitingForBinaryData() {
   Collect.getInstance().getFormController().setIndexWaitingForData(null);
 }
 @Override
 public boolean isWaitingForBinaryData() {
   return mPrompt
       .getIndex()
       .equals(Collect.getInstance().getFormController().getIndexWaitingForData());
 }