private static BitmapSampled cropBitmap(
      Context context,
      Uri loadedImageUri,
      float[] points,
      int degreesRotated,
      boolean fixAspectRatio,
      int aspectRatioX,
      int aspectRatioY,
      int sampleMulti,
      Rect rect,
      int width,
      int height,
      boolean flipHorizontally,
      boolean flipVertically) {
    Bitmap result = null;
    int sampleSize;
    try {
      BitmapFactory.Options options = new BitmapFactory.Options();
      options.inSampleSize =
          sampleSize =
              sampleMulti
                  * calculateInSampleSizeByReqestedSize(rect.width(), rect.height(), width, height);

      Bitmap fullBitmap = decodeImage(context.getContentResolver(), loadedImageUri, options);
      if (fullBitmap != null) {
        try {
          // adjust crop points by the sampling because the image is smaller
          float[] points2 = new float[points.length];
          System.arraycopy(points, 0, points2, 0, points.length);
          for (int i = 0; i < points2.length; i++) {
            points2[i] = points2[i] / options.inSampleSize;
          }

          result =
              cropBitmapObjectWithScale(
                  fullBitmap,
                  points2,
                  degreesRotated,
                  fixAspectRatio,
                  aspectRatioX,
                  aspectRatioY,
                  1,
                  flipHorizontally,
                  flipVertically);
        } finally {
          if (result != fullBitmap) {
            fullBitmap.recycle();
          }
        }
      }
    } catch (OutOfMemoryError e) {
      if (result != null) {
        result.recycle();
      }
      throw e;
    } catch (Exception e) {
      throw new RuntimeException(
          "Failed to load sampled bitmap: " + loadedImageUri + "\r\n" + e.getMessage(), e);
    }
    return new BitmapSampled(result, sampleSize);
  }
Crop bitmap by fully loading the original and then cropping it, fallback in case cropping
region failed.
core/src/main/java/com/theartofdev/edmodo/cropper/BitmapUtils.java