content: package com.yalantis.ucrop.sample;
  
  import android.Manifest;
  import android.content.Intent;
  import android.content.pm.PackageManager;
  import android.graphics.Bitmap;
  import android.net.Uri;
  import android.os.Build;
  import android.os.Bundle;
  import android.support.annotation.NonNull;
  import android.support.v4.app.ActivityCompat;
  import android.text.Editable;
  import android.text.TextWatcher;
  import android.util.Log;
  import android.view.View;
  import android.widget.CheckBox;
  import android.widget.EditText;
  import android.widget.RadioGroup;
  import android.widget.SeekBar;
  import android.widget.TextView;
  import android.widget.Toast;
  
  import com.yalantis.ucrop.UCrop;
  import com.yalantis.ucrop.UCropActivity;
  
  import java.io.File;
  
  /**
   * Created by Oleksii Shliama (https://github.com/shliama).
   */
  public class SampleActivity extends BaseActivity {
  
      private static final String TAG = "SampleActivity";
  
      private static final int REQUEST_SELECT_PICTURE = 0x01;
      private static final String SAMPLE_CROPPED_IMAGE_NAME = "SampleCropImage.jpeg";
  
      private RadioGroup mRadioGroupAspectRatio, mRadioGroupCompressionSettings;
      private EditText mEditTextMaxWidth, mEditTextMaxHeight;
      private EditText mEditTextRatioX, mEditTextRatioY;
      private CheckBox mCheckBoxMaxSize;
      private SeekBar mSeekBarQuality;
      private TextView mTextViewQuality;
  
      private Uri mDestinationUri;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_sample);
  
          mDestinationUri = Uri.fromFile(new File(getCacheDir(), SAMPLE_CROPPED_IMAGE_NAME));
  
          setupUI();
      }
  
      @Override
      public void onActivityResult(int requestCode, int resultCode, Intent data) {
          if (resultCode == RESULT_OK) {
              if (requestCode == REQUEST_SELECT_PICTURE) {
                  final Uri selectedUri = data.getData();
                  if (selectedUri != null) {
                      startCropActivity(data.getData());
                  } else {
                      Toast.makeText(SampleActivity.this, R.string.toast_cannot_retrieve_selected_image, Toast.LENGTH_SHORT).show();
                  }
              } else if (requestCode == UCrop.REQUEST_CROP) {
                  handleCropResult(data);
              }
          }
          if (resultCode == UCrop.RESULT_ERROR) {
              handleCropError(data);
          }
      }
  
      /**
       * Callback received when a permissions request has been completed.
       */
      @Override
      public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
          switch (requestCode) {
              case REQUEST_STORAGE_READ_ACCESS_PERMISSION:
                  if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                      pickFromGallery();
                  }
                  break;
              default:
                  super.onRequestPermissionsResult(requestCode, permissions, grantResults);
          }
      }
  
      private void setupUI() {
          findViewById(R.id.button_crop).setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  pickFromGallery();
              }
          });
  
          mRadioGroupAspectRatio = ((RadioGroup) findViewById(R.id.radio_group_aspect_ratio));
          mRadioGroupCompressionSettings = ((RadioGroup) findViewById(R.id.radio_group_compression_settings));
          mCheckBoxMaxSize = ((CheckBox) findViewById(R.id.checkbox_max_size));
          mEditTextRatioX = ((EditText) findViewById(R.id.edit_text_ratio_x));
          mEditTextRatioY = ((EditText) findViewById(R.id.edit_text_ratio_y));
          mEditTextMaxWidth = ((EditText) findViewById(R.id.edit_text_max_width));
          mEditTextMaxHeight = ((EditText) findViewById(R.id.edit_text_max_height));
          mSeekBarQuality = ((SeekBar) findViewById(R.id.seekbar_quality));
          mTextViewQuality = ((TextView) findViewById(R.id.text_view_quality));
  
          mRadioGroupAspectRatio.check(R.id.radio_dynamic);
          mEditTextRatioX.addTextChangedListener(mAspectRatioTextWatcher);
          mEditTextRatioY.addTextChangedListener(mAspectRatioTextWatcher);
          mRadioGroupCompressionSettings.check(R.id.radio_jpeg);
          mSeekBarQuality.setProgress(UCropActivity.DEFAULT_COMPRESS_QUALITY);
          mTextViewQuality.setText(String.format(getString(R.string.format_quality_d), mSeekBarQuality.getProgress()));
          mSeekBarQuality.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
              @Override
              public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                  mTextViewQuality.setText(String.format(getString(R.string.format_quality_d), progress));
              }
  
              @Override
              public void onStartTrackingTouch(SeekBar seekBar) {
  
              }
  
              @Override
              public void onStopTrackingTouch(SeekBar seekBar) {
  
              }
          });
      }
  
      private TextWatcher mAspectRatioTextWatcher = new TextWatcher() {
          @Override
          public void beforeTextChanged(CharSequence s, int start, int count, int after) {
              mRadioGroupAspectRatio.clearCheck();
          }
  
          @Override
          public void onTextChanged(CharSequence s, int start, int before, int count) {
  
          }
  
          @Override
          public void afterTextChanged(Editable s) {
  
          }
      };
  
      private void pickFromGallery() {
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN // Permission was added in API Level 16
                  && ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
                  != PackageManager.PERMISSION_GRANTED) {
              requestPermission(Manifest.permission.READ_EXTERNAL_STORAGE,
                      getString(R.string.permission_read_storage_rationale),
                      REQUEST_STORAGE_READ_ACCESS_PERMISSION);
          } else {
              Intent intent = new Intent();
              intent.setType("image/*");
              intent.setAction(Intent.ACTION_GET_CONTENT);
              intent.addCategory(Intent.CATEGORY_OPENABLE);
              startActivityForResult(Intent.createChooser(intent, getString(R.string.label_select_picture)), REQUEST_SELECT_PICTURE);
          }
      }
  
      private void startCropActivity(@NonNull Uri uri) {
          UCrop uCrop = UCrop.of(uri, mDestinationUri);
  
          uCrop = basisConfig(uCrop);
          uCrop = advancedConfig(uCrop);
  
          uCrop.start(SampleActivity.this);
      }
  
      /**
       * In most cases you need only to set crop aspect ration and max size for resulting image.
       *
       * @param uCrop - ucrop builder instance
       * @return - ucrop builder instance
       */
      private UCrop basisConfig(@NonNull UCrop uCrop) {
          switch (mRadioGroupAspectRatio.getCheckedRadioButtonId()) {
              case R.id.radio_origin:
                  uCrop = uCrop.useSourceImageAspectRatio();
                  break;
              case R.id.radio_square:
                  uCrop = uCrop.withAspectRatio(1, 1);
                  break;
              case R.id.radio_dynamic:
                  // do nothing
                  break;
              default:
                  try {
                      float ratioX = Float.valueOf(mEditTextRatioX.getText().toString().trim());
                      float ratioY = Float.valueOf(mEditTextRatioY.getText().toString().trim());
                      if (ratioX > 0 && ratioY > 0) {
                          uCrop = uCrop.withAspectRatio(ratioX, ratioY);
                      }
                  } catch (NumberFormatException e) {
                      Log.e(TAG, "Number please", e);
                  }
                  break;
          }
  
          if (mCheckBoxMaxSize.isChecked()) {
              try {
                  int maxWidth = Integer.valueOf(mEditTextMaxWidth.getText().toString().trim());
                  int maxHeight = Integer.valueOf(mEditTextMaxHeight.getText().toString().trim());
                  if (maxWidth > 0 && maxHeight > 0) {
                      uCrop = uCrop.withMaxResultSize(maxWidth, maxHeight);
                  }
              } catch (NumberFormatException e) {
                  Log.e(TAG, "Number please", e);
              }
          }
  
          return uCrop;
      }
  
      /**
       * Sometimes you want to adjust more options, it's done via {@link com.yalantis.ucrop.UCrop.Options} class.
       *
       * @param uCrop - ucrop builder instance
       * @return - ucrop builder instance
       */
      private UCrop advancedConfig(@NonNull UCrop uCrop) {
          UCrop.Options options = new UCrop.Options();
  
          switch (mRadioGroupCompressionSettings.getCheckedRadioButtonId()) {
              case R.id.radio_png:
                  options.setCompressionFormat(Bitmap.CompressFormat.PNG);
                  break;
              case R.id.radio_webp:
                  options.setCompressionFormat(Bitmap.CompressFormat.WEBP);
                  break;
              case R.id.radio_jpeg:
              default:
                  options.setCompressionFormat(Bitmap.CompressFormat.JPEG);
                  break;
          }
          options.setCompressionQuality(mSeekBarQuality.getProgress());
  
          /*
          If you want to configure how gestures work for all UCropActivity tabs
  
          options.setAllowedGestures(UCropActivity.SCALE, UCropActivity.ROTATE, UCropActivity.ALL);
          * */
  
          /*
          This sets max size for bitmap that will be decoded from source Uri.
          More size - more memory allocation, default implementation uses screen diagonal.
  
          options.setMaxBitmapSize(640);
          * */
  
  
         /*
  
          Tune everything (ﾉ◕ヮ◕)ﾉ*:･ﾟ✧
  
          options.setMaxScaleMultiplier(5);
          options.setImageToCropBoundsAnimDuration(666);
          options.setDimmedLayerColor(Color.CYAN);
          options.setOvalDimmedLayer(true);
          options.setShowCropFrame(false);
          options.setCropGridStrokeWidth(20);
          options.setCropGridColor(Color.GREEN);
          options.setCropGridColumnCount(2);
          options.setCropGridRowCount(1);
  
          // Color palette
          options.setToolbarColor(ContextCompat.getColor(this, R.color.your_color_res));
          options.setStatusBarColor(ContextCompat.getColor(this, R.color.your_color_res));
          options.setActiveWidgetColor(ContextCompat.getColor(this, R.color.your_color_res));
  		options.setToolbarTitleTextColor(ContextCompat.getColor(this, R.color.your_color_res));
  
         */
  
          return uCrop.withOptions(options);
      }
  
      private void handleCropResult(@NonNull Intent result) {
          final Uri resultUri = UCrop.getOutput(result);
          if (resultUri != null) {
              ResultActivity.startWithUri(SampleActivity.this, resultUri);
          } else {
              Toast.makeText(SampleActivity.this, R.string.toast_cannot_retrieve_cropped_image, Toast.LENGTH_SHORT).show();
          }
      }
  
      @SuppressWarnings("ThrowableResultOfMethodCallIgnored")
      private void handleCropError(@NonNull Intent result) {
          final Throwable cropError = UCrop.getError(result);
          if (cropError != null) {
              Log.e(TAG, "handleCropError: ", cropError);
              Toast.makeText(SampleActivity.this, cropError.getMessage(), Toast.LENGTH_LONG).show();
          } else {
              Toast.makeText(SampleActivity.this, R.string.toast_unexpected_error, Toast.LENGTH_SHORT).show();
          }
      }
  
  }
  