Skip to content

Commit

Permalink
Merge pull request Yalantis#693 from zalesskyi/fix/uCrop_non_native_r…
Browse files Browse the repository at this point in the history
…esult_size

Fixed cropped image size.
  • Loading branch information
warko-san committed Aug 20, 2020
2 parents 411a11f + 6b2336e commit 281c8e6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
1 change: 1 addition & 0 deletions sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<application
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
android:requestLegacyExternalStorage="true"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<provider
Expand Down
20 changes: 14 additions & 6 deletions ucrop/src/main/java/com/yalantis/ucrop/task/BitmapCropTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
import android.os.AsyncTask;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.exifinterface.media.ExifInterface;

import com.yalantis.ucrop.callback.BitmapCropCallback;
import com.yalantis.ucrop.model.CropParameters;
import com.yalantis.ucrop.model.ExifInfo;
Expand All @@ -16,16 +20,14 @@
import com.yalantis.ucrop.util.FileUtils;
import com.yalantis.ucrop.util.ImageHeaderParser;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.ref.WeakReference;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.exifinterface.media.ExifInterface;

/**
* Crops part of image that fills the crop bounds.
* <p/>
Expand Down Expand Up @@ -166,12 +168,18 @@ private void saveImage(@NonNull Bitmap croppedBitmap) throws FileNotFoundExcepti
}

OutputStream outputStream = null;
ByteArrayOutputStream outStream = null;
try {
outputStream = context.getContentResolver().openOutputStream(Uri.fromFile(new File(mImageOutputPath)));
croppedBitmap.compress(mCompressFormat, mCompressQuality, outputStream);
outputStream = new FileOutputStream(new File(mImageOutputPath), false);
outStream = new ByteArrayOutputStream();
croppedBitmap.compress(mCompressFormat, mCompressQuality, outStream);
outputStream.write(outStream.toByteArray());
croppedBitmap.recycle();
} catch (IOException exc) {
Log.e(TAG, exc.getLocalizedMessage());
} finally {
BitmapLoadUtils.close(outputStream);
BitmapLoadUtils.close(outStream);
}
}

Expand Down
11 changes: 11 additions & 0 deletions ucrop/src/main/java/com/yalantis/ucrop/task/BitmapLoadTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public class BitmapLoadTask extends AsyncTask<Void, Void, BitmapLoadTask.BitmapW

private static final String TAG = "BitmapWorkerTask";

private static final int MAX_BITMAP_SIZE = 100 * 1024 * 1024; // 100 MB

private final Context mContext;
private Uri mInputUri;
private Uri mOutputUri;
Expand Down Expand Up @@ -106,6 +108,7 @@ protected BitmapWorkerResult doInBackground(Void... params) {
} finally {
BitmapLoadUtils.close(stream);
}
if (checkSize(decodeSampledBitmap, options)) continue;
decodeAttemptSuccess = true;
} catch (OutOfMemoryError error) {
Log.e(TAG, "doInBackground: BitmapFactory.decodeFileDescriptor: ", error);
Expand Down Expand Up @@ -243,4 +246,12 @@ protected void onPostExecute(@NonNull BitmapWorkerResult result) {
}
}

private boolean checkSize(Bitmap bitmap, BitmapFactory.Options options) {
int bitmapSize = bitmap != null ? bitmap.getByteCount() : 0;
if (bitmapSize > MAX_BITMAP_SIZE) {
options.inSampleSize *= 2;
return true;
}
return false;
}
}

0 comments on commit 281c8e6

Please sign in to comment.