private void saveOutput(Bitmap croppedImage) {
    if (saveUri != null) {
      OutputStream outputStream = null;
      try {
        outputStream = getContentResolver().openOutputStream(saveUri);
        if (outputStream != null) {
          croppedImage.compress(Bitmap.CompressFormat.JPEG, 90, outputStream);
        }
      } catch (IOException e) {
        setResultException(e);
        Log.e("Cannot open file: " + saveUri, e);
      } finally {
        CropUtil.closeSilently(outputStream);
      }

      CropUtil.copyExifRotation(
          CropUtil.getFromMediaUri(this, getContentResolver(), sourceUri),
          CropUtil.getFromMediaUri(this, getContentResolver(), saveUri));

      setResultUri(saveUri);
    }

    final Bitmap b = croppedImage;
    handler.post(
        new Runnable() {
          public void run() {
            imageView.clear();
            b.recycle();
          }
        });

    finish();
  }
  private void loadInput() {
    Intent intent = getIntent();
    Bundle extras = intent.getExtras();

    if (extras != null) {
      aspectX = extras.getInt(Crop.Extra.ASPECT_X);
      aspectY = extras.getInt(Crop.Extra.ASPECT_Y);
      maxX = extras.getInt(Crop.Extra.MAX_X);
      maxY = extras.getInt(Crop.Extra.MAX_Y);
      saveUri = extras.getParcelable(MediaStore.EXTRA_OUTPUT);
    }

    sourceUri = intent.getData();
    if (sourceUri != null) {
      exifRotation =
          CropUtil.getExifRotation(CropUtil.getFromMediaUri(this, getContentResolver(), sourceUri));

      InputStream is = null;
      try {
        sampleSize = calculateBitmapSampleSize(sourceUri);
        is = getContentResolver().openInputStream(sourceUri);
        BitmapFactory.Options option = new BitmapFactory.Options();
        option.inSampleSize = sampleSize;
        rotateBitmap = new RotateBitmap(BitmapFactory.decodeStream(is, null, option), exifRotation);
      } catch (IOException e) {
        Log.e("Error reading image: " + e.getMessage(), e);
        setResultException(e);
      } catch (OutOfMemoryError e) {
        Log.e("OOM reading image: " + e.getMessage(), e);
        setResultException(e);
      } finally {
        CropUtil.closeSilently(is);
      }
    }
  }
示例#3
0
  private void saveOutput(Bitmap croppedImage) {
    if (saveUri != null) {
      OutputStream outputStream = null;
      try {
        outputStream = getContentResolver().openOutputStream(saveUri);
        if (outputStream != null) {

          if (exifRotation > 0) {
            try {
              Matrix matrix = new Matrix();
              matrix.reset();
              matrix.postRotate(exifRotation);
              Bitmap bMapRotate =
                  Bitmap.createBitmap(
                      croppedImage,
                      0,
                      0,
                      croppedImage.getWidth(),
                      croppedImage.getHeight(),
                      matrix,
                      true);
              bMapRotate.compress(Bitmap.CompressFormat.PNG, 70, outputStream);

            } catch (Exception e) {
              e.printStackTrace();
              croppedImage.compress(Bitmap.CompressFormat.PNG, 70, outputStream);
            } finally {
              if (croppedImage != null && !croppedImage.isRecycled()) {
                croppedImage.recycle();
              }
            }
          } else {
            croppedImage.compress(Bitmap.CompressFormat.PNG, 70, outputStream);
          }
        }

      } catch (IOException e) {
        setResultException(e);
        Log.e("Cannot open file: " + saveUri, e);
      } finally {
        CropUtil.closeSilently(outputStream);
      }

      if (!IN_MEMORY_CROP) {
        // In-memory crop negates the rotation
        CropUtil.copyExifRotation(
            CropUtil.getFromMediaUri(getContentResolver(), sourceUri),
            CropUtil.getFromMediaUri(getContentResolver(), saveUri));
      }

      setResultUri(saveUri);
    }

    final Bitmap b = croppedImage;
    handler.post(
        new Runnable() {

          public void run() {
            imageView.clear();
            b.recycle();
          }
        });

    finish();
  }