private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    // Ensure that there's a camera activity to handle the intent
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
      photoFile = null;
      try {
        photoFile = createImageFile();
      } catch (IOException ex) {
        Log.e(LOG, "F**k!", ex);
        Util.showErrorToast(ctx, getString(R.string.file_error));
        return;
      }
      if (photoFile != null) {
        Log.w(LOG, "dispatchTakePictureIntent - start pic intent");

        if (mLocationClient.isConnected()) {
          Log.w(LOG, "## requesting mLocationClient updates before picture taken");
          mLocationClient.requestLocationUpdates(mLocationRequest, this);
        } else {
          mLocationClient.connect();
        }
        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
        startActivityForResult(takePictureIntent, CAPTURE_IMAGE);
      }
    }
  }
  public void setLocation(Location location) {
    if (projectSite == null) return;
    this.location = location;
    txtLat.setText("" + location.getLatitude());
    txtLng.setText("" + location.getLongitude());
    txtAccuracy.setText("" + location.getAccuracy());

    if (location.getAccuracy() == seekBar.getProgress()
        || location.getAccuracy() < seekBar.getProgress()) {
      listener.onEndScanRequested();
      isScanning = false;
      stopRotatingLogo();
      chronometer.stop();
      resetLogo();
      btnScan.setText(ctx.getString(R.string.start_scan));
      btnSave.setVisibility(View.VISIBLE);
      projectSite.setLatitude(location.getLatitude());
      projectSite.setLongitude(location.getLongitude());
      projectSite.setAccuracy(location.getAccuracy());
      Util.expand(btnSave, 200, null);
      Log.d(LOG, "----------- onEndScanRequested - stopped scanning");
    }
    Util.flashSeveralTimes(hero, 300, 1, null);
  }
  private void loadImage() {
    StringBuilder sb = new StringBuilder();
    PhotoUploadDTO dto = photoCache.getPhotoUploadList().get(index);
    sb.append(Statics.IMAGE_URL).append(dto.getUri());
    // Picasso.with(ctx).load(sb.toString()).into(imageView);
    ImageLoader.getInstance()
        .displayImage(
            sb.toString(),
            imageView,
            new ImageLoadingListener() {
              @Override
              public void onLoadingStarted(String s, View view) {}

              @Override
              public void onLoadingFailed(String s, View view, FailReason failReason) {}

              @Override
              public void onLoadingComplete(String s, View view, Bitmap bitmap) {

                if (bitmap.getWidth() > bitmap.getHeight()) {
                  isLandscape = true;
                  // setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                } else {
                  isLandscape = false;
                  // setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                }
                Log.e(
                    LOG,
                    "------ onLoadingComplete - height: "
                        + bitmap.getHeight()
                        + " width: "
                        + bitmap.getWidth()
                        + " isLandscape: "
                        + isLandscape);
              }

              @Override
              public void onLoadingCancelled(String s, View view) {}
            });
    txtNumber.setText("" + (index + 1));
    txtDate.setText(sdf.format(dto.getDateTaken()));
    animate();
    Util.animateRotationY(txtNext, 500);
  }
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_image);
    ctx = getApplicationContext();
    if (savedInstanceState != null) {
      index = savedInstanceState.getInt("index");
      projectSite = (ProjectSiteDTO) savedInstanceState.getSerializable("projectSite");
      project = (ProjectDTO) savedInstanceState.getSerializable("project");
    } else {
      project = (ProjectDTO) getIntent().getSerializableExtra("project");
      projectSite = (ProjectSiteDTO) getIntent().getSerializableExtra("projectSite");
      index = getIntent().getIntExtra("index", 0);
    }
    setFields();
    txtNumber.setText("" + (index + 1));

    photoCache = new PhotoCache();
    StringBuilder sb = new StringBuilder();
    sb.append(Statics.IMAGE_URL);
    if (projectSite != null) {
      photoCache.setPhotoUploadList(projectSite.getPhotoUploadList());
      PhotoUploadDTO dto = photoCache.getPhotoUploadList().get(index);
      sb.append(dto.getUri());
      txtTitle.setText(projectSite.getProjectName());
      txtSubTitle.setText(projectSite.getProjectSiteName());
      txtDate.setText(sdf.format(dto.getDateTaken()));
    }
    if (project != null) {
      photoCache.setPhotoUploadList(project.getPhotoUploadList());
      PhotoUploadDTO dto = photoCache.getPhotoUploadList().get(index);
      sb.append(dto.getUri());
      txtTitle.setText(project.getProjectName());
      txtDate.setText(sdf.format(dto.getDateTaken()));
      txtSubTitle.setVisibility(View.GONE);
    }
    url = sb.toString();
    Picasso.with(ctx).load(url).into(imageView);

    Util.animateScaleY(imageView, 200);
    setHeader();
  }
  private File createImageFile() throws IOException {
    // Create an image file name
    String imageFileName = "pic" + System.currentTimeMillis();
    File storageDir;
    if (Util.hasStorage(true)) {
      Log.i(LOG, "###### get file from getExternalStoragePublicDirectory");
      storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    } else {
      Log.i(LOG, "###### get file from getDataDirectory");
      storageDir = Environment.getDataDirectory();
    }

    File image =
        File.createTempFile(
            imageFileName, /* prefix */ ".jpg", /* suffix */ storageDir /* directory */);

    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = "file:" + image.getAbsolutePath();
    return image;
  }
 @Override
 protected void onPostExecute(Integer result) {
   if (result > 0) {
     Util.showErrorToast(ctx, ctx.getResources().getString(R.string.camera_error));
     return;
   }
   if (thumbUri != null) {
     pictureChanged = true;
     try {
       image.setImageBitmap(bitmapForScreen);
       uploadPhotos();
       //                    if (bitmapForScreen.getWidth() > bitmapForScreen.getHeight()) {
       //
       // setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
       //                    } else {
       //
       // setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
       //                    }
     } catch (Exception e) {
       e.printStackTrace();
     }
   }
 }
 private void animate() {
   Util.animateSlideRight(imageView, 500);
   Util.animateRotationY(txtNumber, 500);
 }
    @Override
    protected Integer doInBackground(Void... voids) {
      Log.w(LOG, "## PhotoTask starting doInBackground, file length: " + photoFile.length());
      pictureChanged = false;
      ExifInterface exif = null;
      if (photoFile == null || photoFile.length() == 0) {
        Log.e(LOG, "----- photoFile is null or length 0, exiting");
        return 99;
      }
      fileUri = Uri.fromFile(photoFile);
      if (fileUri != null) {
        try {
          exif = new ExifInterface(photoFile.getAbsolutePath());
          String orient = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
          Log.i(LOG, "@@@@@@@@@@@@@@@@@@@@@@ Orientation says: " + orient);
          float rotate = 0f;
          if (orient.equalsIgnoreCase("6")) {
            rotate = 90f;
            Log.i(LOG, "@@@@@ picture, rotate = " + rotate);
          }
          try {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 2;
            Bitmap bm = BitmapFactory.decodeFile(photoFile.getAbsolutePath(), options);
            if (bm == null) {
              Log.e(LOG, "---> Bitmap is null, file length: " + photoFile.length());
            }
            getLog(bm, "Raw Camera");
            // get thumbnail for upload
            Matrix matrixThumbnail = new Matrix();
            matrixThumbnail.postScale(0.4f, 0.4f);
            // matrixThumbnail.postRotate(rotate);
            Bitmap thumb =
                Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrixThumbnail, true);
            getLog(thumb, "Thumb");

            // get resized "full" size for upload
            Matrix matrixF = new Matrix();
            matrixF.postScale(0.75f, 0.75f);
            // matrixF.postRotate(rotate);
            Bitmap fullBm =
                Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrixF, true);

            getLog(fullBm, "Full");
            // append date and gps coords to bitmap
            // fullBm = ImageUtil.drawTextToBitmap(ctx,fullBm,location);
            // thumb = ImageUtil.drawTextToBitmap(ctx,thumb,location);

            currentFullFile =
                ImageUtil.getFileFromBitmap(fullBm, "m" + System.currentTimeMillis() + ".jpg");
            currentThumbFile =
                ImageUtil.getFileFromBitmap(thumb, "t" + System.currentTimeMillis() + ".jpg");
            bitmapForScreen = ImageUtil.getBitmapFromUri(ctx, Uri.fromFile(currentFullFile));

            Log.e(LOG, "## files created from camera bitmap");

            thumbUri = Uri.fromFile(currentThumbFile);
            fullUri = Uri.fromFile(currentFullFile);
            // write exif data
            Util.writeLocationToExif(currentFullFile.getAbsolutePath(), location);
            Util.writeLocationToExif(currentThumbFile.getAbsolutePath(), location);
            fullBm = null;
            thumb = null;
            bm = null;
            getFileLengths();
          } catch (Exception e) {
            Log.e("pic", "F**k it! unable to process bitmap", e);
            return 9;
          }

        } catch (Exception e) {
          e.printStackTrace();
          return 1;
        }
      }
      return 0;
    }