@Override
 public void onWindowFocusChanged(boolean hasFocus) {
   // TODO Auto-generated method stub
   super.onWindowFocusChanged(hasFocus);
   if (hasFocus) {
     scaleView();
   }
 }
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
      switch (requestCode) {
        case GALLERY:
          try {
            Uri selected_image = data.getData();
            String selected_image_path = getImagePath(selected_image);
            onPhotoTaken(selected_image_path);
          } catch (Exception e) {
            Toast.makeText(this, "Error loading image from Gallery!", Toast.LENGTH_LONG).show();
            e.printStackTrace();
            finish();
          }

          break;
        case CAMERA:
          File file = new File(_path);
          boolean exists = file.exists();
          if (exists) onPhotoTaken(_path);
          else
            Toast.makeText(
                    getBaseContext(),
                    "Something goes wrong while taking picture, please try again.",
                    Toast.LENGTH_SHORT)
                .show();
          break;
        default:
          finish();
          break;
      }

    }
    /** if there is no image, just finish the activity */
    else {
      finish();
    }
  }
 @Override
 protected void onResume() {
   super.onResume();
   if (return_flag) finish();
 }
  @Override
  public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    String pManufacturer = android.os.Build.MANUFACTURER;
    String pModel = android.os.Build.MODEL;

    if ("GT-I9300".equals(pModel) && "samsung".equals(pManufacturer)) {

      RelativeLayout main = (RelativeLayout) findViewById(R.id.relativeLayout_main);
      main.invalidate();

      setContentView(R.layout.activity_camera_crop);

      mImageView = (CroppedImageView) findViewById(R.id.ivCameraCropPhoto);
      mImageView.setDrawingCacheEnabled(true);

      scaleView();

      // Cancel button
      findViewById(R.id.btnCameraCancel)
          .setOnClickListener(
              new OnClickListener() {

                @Override
                public void onClick(View v) {

                  finish();
                  if (getIntent().getStringExtra("type").equals("gallery")) {
                    Intent intent = new Intent(CameraCropActivity.this, CameraCropActivity.class);
                    intent.putExtra("type", "gallery");
                    startActivity(intent);
                  } else {
                    Intent intent = new Intent(CameraCropActivity.this, CameraCropActivity.class);
                    intent.putExtra("type", "camera");
                    startActivity(intent);
                  }
                }
              });

      // Next button
      findViewById(R.id.btnCameraOk)
          .setOnClickListener(
              new OnClickListener() {

                @Override
                public void onClick(View v) {
                  Bitmap resizedBitmap = getBitmapFromView(mImageView);

                  ByteArrayOutputStream bs = new ByteArrayOutputStream();
                  resizedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bs);

                  if (saveBitmapToFile(resizedBitmap, mFilePath) == true) {

                    if (mForProfile == true) {
                      MyProfileActivity.gProfileImage = getBitmapFromView(mImageView);
                      MyProfileActivity.gProfileImagePath = mFilePath;
                    } else if (mForGroup == true) {
                      CreateGroupActivity.gGroupImage = getBitmapFromView(mImageView);
                      CreateGroupActivity.gGroupImagePath = mFilePath;
                    } else if (mForGroupUpdate == true) {
                      GroupProfileActivity.gGroupImage = getBitmapFromView(mImageView);
                      GroupProfileActivity.gGroupImagePath = mFilePath;
                    } else {
                      fileUploadAsync(mFilePath);

                      //									new FileUploadAsync(CameraCropActivity.this)
                      //											.execute(mFilePath);
                      // new
                      // SendMessageAsync(getApplicationContext(),
                      // SendMessageAsync.TYPE_PHOTO)
                      // .execute(resizedBitmap);
                    }
                  } else {
                    Toast.makeText(
                            CameraCropActivity.this, "Failed to send photo", Toast.LENGTH_LONG)
                        .show();
                  }
                }
              });

      File file = new File(_path);
      boolean exists = file.exists();
      if (exists) onPhotoTaken(_path);
      else
        Toast.makeText(
                getBaseContext(),
                "Something went wrong while taking picture, please try again.",
                Toast.LENGTH_SHORT)
            .show();
    }
  }
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_camera_crop);

    getImageIntents();
    mImageView = (CroppedImageView) findViewById(R.id.ivCameraCropPhoto);
    mImageView.setDrawingCacheEnabled(true);

    return_flag = false;

    Button btnBack = (Button) findViewById(R.id.btnBack);
    btnBack.setOnClickListener(
        new OnClickListener() {

          @Override
          public void onClick(View v) {
            finish();
          }
        });

    // Cancel button
    findViewById(R.id.btnCameraCancel)
        .setOnClickListener(
            new OnClickListener() {

              @Override
              public void onClick(View v) {
                finish();
                if (getIntent().getStringExtra("type").equals("gallery")) {
                  Intent intent = new Intent(CameraCropActivity.this, CameraCropActivity.class);
                  intent.putExtra("type", "gallery");
                  startActivity(intent);
                } else {
                  Intent intent = new Intent(CameraCropActivity.this, CameraCropActivity.class);
                  intent.putExtra("type", "camera");
                  startActivity(intent);
                }
              }
            });

    // Next button
    findViewById(R.id.btnCameraOk)
        .setOnClickListener(
            new OnClickListener() {

              @Override
              public void onClick(View v) {
                Bitmap resizedBitmap = getBitmapFromView(mImageView);
                ByteArrayOutputStream bs = new ByteArrayOutputStream();
                resizedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bs);

                if (saveBitmapToFile(resizedBitmap, mFilePath) == true) {

                  if (mForProfile == true) {
                    MyProfileActivity.gProfileImage = getBitmapFromView(mImageView);
                    MyProfileActivity.gProfileImagePath = mFilePath;
                    finish();
                  } else if (mForGroup == true) {
                    CreateGroupActivity.gGroupImage = getBitmapFromView(mImageView);
                    CreateGroupActivity.gGroupImagePath = mFilePath;
                    finish();
                  } else if (mForGroupUpdate == true) {
                    GroupProfileActivity.gGroupImage = getBitmapFromView(mImageView);
                    GroupProfileActivity.gGroupImagePath = mFilePath;
                    finish();
                  } else {
                    fileUploadAsync(mFilePath);

                    //								new FileUploadAsync(CameraCropActivity.this)
                    //										.execute(mFilePath);
                    // new SendMessageAsync(getApplicationContext(),
                    // SendMessageAsync.TYPE_PHOTO)
                    // .execute(resizedBitmap);
                  }

                } else {
                  Toast.makeText(CameraCropActivity.this, "Failed to send photo", Toast.LENGTH_LONG)
                      .show();
                }
              }
            });
  }