@Kroll.method
  public void openPhotoGallery(KrollDict options) {
    KrollFunction successCallback = null;
    KrollFunction cancelCallback = null;
    KrollFunction errorCallback = null;

    if (options.containsKey("success")) {
      successCallback = (KrollFunction) options.get("success");
    }
    if (options.containsKey("cancel")) {
      cancelCallback = (KrollFunction) options.get("cancel");
    }
    if (options.containsKey("error")) {
      errorCallback = (KrollFunction) options.get("error");
    }

    final KrollFunction fSuccessCallback = successCallback;
    final KrollFunction fCancelCallback = cancelCallback;
    final KrollFunction fErrorCallback = errorCallback;

    Log.d(TAG, "openPhotoGallery called", Log.DEBUG_MODE);

    Activity activity = TiApplication.getInstance().getCurrentActivity();
    TiActivitySupport activitySupport = (TiActivitySupport) activity;

    TiIntentWrapper galleryIntent = new TiIntentWrapper(new Intent());
    galleryIntent.getIntent().setAction(Intent.ACTION_PICK);
    galleryIntent.getIntent().setType("image/*");
    galleryIntent.getIntent().addCategory(Intent.CATEGORY_DEFAULT);
    galleryIntent.setWindowId(TiIntentWrapper.createActivityName("GALLERY"));

    final int code = activitySupport.getUniqueResultCode();
    activitySupport.launchActivityForResult(
        galleryIntent.getIntent(),
        code,
        new TiActivityResultHandler() {

          public void onResult(Activity activity, int requestCode, int resultCode, Intent data) {
            Log.e(TAG, "OnResult called: " + resultCode);
            if (resultCode == Activity.RESULT_CANCELED) {
              if (fCancelCallback != null) {
                KrollDict response = new KrollDict();
                response.putCodeAndMessage(NO_ERROR, null);
                fCancelCallback.callAsync(getKrollObject(), response);
              }

            } else {
              String path = data.getDataString();
              try {
                if (fSuccessCallback != null) {
                  fSuccessCallback.callAsync(
                      getKrollObject(), createDictForImage(path, "image/jpeg"));
                }

              } catch (OutOfMemoryError e) {
                String msg = "Not enough memory to get image: " + e.getMessage();
                Log.e(TAG, msg);
                if (fErrorCallback != null) {
                  fErrorCallback.callAsync(
                      getKrollObject(), createErrorResponse(UNKNOWN_ERROR, msg));
                }
              }
            }
          }

          public void onError(Activity activity, int requestCode, Exception e) {
            String msg = "Gallery problem: " + e.getMessage();
            Log.e(TAG, msg, e);
            if (fErrorCallback != null) {
              fErrorCallback.callAsync(getKrollObject(), createErrorResponse(UNKNOWN_ERROR, msg));
            }
          }
        });
  }
  @Kroll.method
  public void previewImage(KrollDict options) {
    Activity activity = TiApplication.getAppCurrentActivity();
    if (activity == null) {
      Log.w(TAG, "Unable to get current activity for previewImage.", Log.DEBUG_MODE);
      return;
    }

    KrollFunction successCallback = null;
    KrollFunction errorCallback = null;
    TiBlob image = null;

    if (options.containsKey("success")) {
      successCallback = (KrollFunction) options.get("success");
    }
    if (options.containsKey("error")) {
      errorCallback = (KrollFunction) options.get("error");
    }
    if (options.containsKey("image")) {
      image = (TiBlob) options.get("image");
    }

    if (image == null) {
      if (errorCallback != null) {
        errorCallback.callAsync(
            getKrollObject(), createErrorResponse(UNKNOWN_ERROR, "Missing image property"));
      }
    }

    TiBaseFile f = (TiBaseFile) image.getData();

    final KrollFunction fSuccessCallback = successCallback;
    final KrollFunction fErrorCallback = errorCallback;

    Log.d(TAG, "openPhotoGallery called", Log.DEBUG_MODE);

    TiActivitySupport activitySupport = (TiActivitySupport) activity;

    Intent intent = new Intent(Intent.ACTION_VIEW);
    TiIntentWrapper previewIntent = new TiIntentWrapper(intent);
    String mimeType = image.getMimeType();

    if (mimeType != null && mimeType.length() > 0) {
      intent.setDataAndType(Uri.parse(f.nativePath()), mimeType);
    } else {
      intent.setData(Uri.parse(f.nativePath()));
    }

    previewIntent.setWindowId(TiIntentWrapper.createActivityName("PREVIEW"));

    final int code = activitySupport.getUniqueResultCode();
    activitySupport.launchActivityForResult(
        intent,
        code,
        new TiActivityResultHandler() {

          public void onResult(Activity activity, int requestCode, int resultCode, Intent data) {
            Log.e(TAG, "OnResult called: " + resultCode);
            if (fSuccessCallback != null) {
              KrollDict response = new KrollDict();
              response.putCodeAndMessage(NO_ERROR, null);
              fSuccessCallback.callAsync(getKrollObject(), response);
            }
          }

          public void onError(Activity activity, int requestCode, Exception e) {
            String msg = "Gallery problem: " + e.getMessage();
            Log.e(TAG, msg, e);
            if (fErrorCallback != null) {
              fErrorCallback.callAsync(getKrollObject(), createErrorResponse(UNKNOWN_ERROR, msg));
            }
          }
        });
  }
 @Override
 public void run() {
   code = activitySupport.getUniqueResultCode();
   activitySupport.launchActivityForResult(cameraIntent, code, this);
 }