public static File createGalleryImageFile() {
    File pictureDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    File appPictureDir = new File(pictureDir, TiApplication.getInstance().getAppInfo().getName());
    if (!appPictureDir.exists()) {
      if (!appPictureDir.mkdirs()) {
        Log.e(TAG, "Failed to create application gallery directory.");
        return null;
      }
    }

    File imageFile;
    try {
      imageFile = TiFileHelper.getInstance().getTempFile(appPictureDir, ".jpg", false);

    } catch (IOException e) {
      Log.e(TAG, "Failed to create gallery image file: " + e.getMessage());
      return null;
    }

    return imageFile;
  }
 private Drawable getDrawableFromUrl(String url) {
   TiUrl imageUrl = new TiUrl((String) url);
   TiFileHelper tfh = new TiFileHelper(TiApplication.getInstance());
   return tfh.loadDrawable(imageUrl.resolve(), false);
 }
 public Drawable loadDrawable(String url) {
   if (tfh == null) {
     tfh = new TiFileHelper(tiContext.getActivity());
   }
   return tfh.loadDrawable(url, false);
 }
  @Kroll.method
  public void showCamera(@SuppressWarnings("rawtypes") HashMap options) {
    Activity activity = TiApplication.getInstance().getCurrentActivity();

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

    KrollFunction successCallback = null;
    KrollFunction cancelCallback = null;
    KrollFunction errorCallback = null;
    boolean autohide = true;
    boolean saveToPhotoGallery = false;

    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");
    }

    Object autohideOption = options.get("autohide");
    if (autohideOption != null) {
      autohide = TiConvert.toBoolean(autohideOption);
    }

    Object saveToPhotoGalleryOption = options.get("saveToPhotoGallery");
    if (saveToPhotoGalleryOption != null) {
      saveToPhotoGallery = TiConvert.toBoolean(saveToPhotoGalleryOption);
    }

    // Use our own custom camera activity when an overlay is provided.
    if (options.containsKey("overlay")) {
      TiCameraActivity.overlayProxy = (TiViewProxy) options.get("overlay");

      TiCameraActivity.callbackContext = getKrollObject();
      TiCameraActivity.successCallback = successCallback;
      TiCameraActivity.errorCallback = errorCallback;
      TiCameraActivity.cancelCallback = cancelCallback;
      TiCameraActivity.saveToPhotoGallery = saveToPhotoGallery;
      TiCameraActivity.whichCamera = CAMERA_REAR; // default.

      // This option is only applicable when running the custom
      // TiCameraActivity, since we can't direct the built-in
      // Activity to open a specific camera.
      Object whichCamera = options.get("whichCamera");
      if (whichCamera != null) {
        TiCameraActivity.whichCamera = TiConvert.toInt(whichCamera);
      }
      TiCameraActivity.autohide = autohide;

      Intent intent = new Intent(activity, TiCameraActivity.class);
      activity.startActivity(intent);
      return;
    }

    Camera camera = null;
    try {
      camera = Camera.open();
      if (camera != null) {
        camera.release();
        camera = null;
      }

    } catch (Throwable t) {
      if (camera != null) {
        camera.release();
      }

      if (errorCallback != null) {
        errorCallback.call(
            getKrollObject(),
            new Object[] {createErrorResponse(NO_CAMERA, "Camera not available.")});
      }

      return;
    }

    TiActivitySupport activitySupport = (TiActivitySupport) activity;
    TiFileHelper tfh = TiFileHelper.getInstance();

    TiIntentWrapper cameraIntent = new TiIntentWrapper(new Intent());
    if (TiCameraActivity.overlayProxy == null) {
      cameraIntent.getIntent().setAction(MediaStore.ACTION_IMAGE_CAPTURE);
      cameraIntent.getIntent().addCategory(Intent.CATEGORY_DEFAULT);
    } else {
      cameraIntent
          .getIntent()
          .setClass(TiApplication.getInstance().getBaseContext(), TiCameraActivity.class);
    }

    cameraIntent.setWindowId(TiIntentWrapper.createActivityName("CAMERA"));
    PackageManager pm = (PackageManager) activity.getPackageManager();
    List<ResolveInfo> activities =
        pm.queryIntentActivities(cameraIntent.getIntent(), PackageManager.MATCH_DEFAULT_ONLY);

    // See if it's the HTC camera app
    boolean isHTCCameraApp = false;

    for (ResolveInfo rs : activities) {
      try {
        if (rs.activityInfo.applicationInfo.sourceDir.contains("HTC")
            || Build.MANUFACTURER.equals("HTC")) {
          isHTCCameraApp = true;
          break;
        }
      } catch (NullPointerException e) {
        // Ignore
      }
    }

    File imageDir = null;
    File imageFile = null;

    try {
      if (saveToPhotoGallery) {
        // HTC camera application will create its own gallery image
        // file.
        if (!isHTCCameraApp) {
          imageFile = createGalleryImageFile();
        }

      } else {
        if (activity.getIntent() != null) {
          String name = TiApplication.getInstance().getAppInfo().getName();
          // For HTC cameras, specifying the directory from
          // getExternalStorageDirectory is /mnt/sdcard and
          // using that path prevents the gallery from recognizing it.
          // To avoid this we use /sdcard instead
          // (this is a legacy path we've been using)
          if (isHTCCameraApp) {
            imageDir = new File(PHOTO_DCIM_CAMERA, name);
          } else {
            File rootsd = Environment.getExternalStorageDirectory();
            imageDir = new File(rootsd.getAbsolutePath() + "/dcim/Camera/", name);
          }
          if (!imageDir.exists()) {
            imageDir.mkdirs();
            if (!imageDir.exists()) {
              Log.w(TAG, "Attempt to create '" + imageDir.getAbsolutePath() + "' failed silently.");
            }
          }

        } else {
          imageDir = tfh.getDataDirectory(false);
        }

        imageFile = tfh.getTempFile(imageDir, ".jpg", true);
      }

    } catch (IOException e) {
      Log.e(TAG, "Unable to create temp file", e);
      if (errorCallback != null) {
        errorCallback.callAsync(
            getKrollObject(), createErrorResponse(UNKNOWN_ERROR, e.getMessage()));
      }

      return;
    }

    // Get the taken date for the last image in EXTERNAL_CONTENT_URI.
    String[] projection = {Images.ImageColumns.DATE_TAKEN};
    String dateTaken = null;
    Cursor c =
        activity
            .getContentResolver()
            .query(
                Images.Media.EXTERNAL_CONTENT_URI,
                projection,
                null,
                null,
                Images.ImageColumns.DATE_TAKEN);
    if (c != null) {
      if (c.moveToLast()) {
        dateTaken = c.getString(0);
      }
      c.close();
      c = null;
    }

    CameraResultHandler resultHandler = new CameraResultHandler();
    resultHandler.imageFile = imageFile;
    resultHandler.saveToPhotoGallery = saveToPhotoGallery;
    resultHandler.successCallback = successCallback;
    resultHandler.cancelCallback = cancelCallback;
    resultHandler.errorCallback = errorCallback;
    resultHandler.activitySupport = activitySupport;
    resultHandler.cameraIntent = cameraIntent.getIntent();
    resultHandler.dateTaken_lastImageInExternalContentURI = dateTaken;

    if (imageFile != null) {
      String imageUrl = "file://" + imageFile.getAbsolutePath();
      cameraIntent.getIntent().putExtra(MediaStore.EXTRA_OUTPUT, Uri.parse(imageUrl));
      resultHandler.imageUrl = imageUrl;
    }

    activity.runOnUiThread(resultHandler);
  }
 public Drawable getBackgroundImageDrawable(KrollProxy proxy, String path) {
   String url = proxy.resolveUrl(null, path);
   return TiFileHelper.loadDrawable(url);
 }