OutOfMemoryError-Android Image Crop Issue

2019-06-10 22:33发布

问题:

I am using a cropper tool i found on GitHub. When I implement it on a separate module, it works absolutely fine.But, When I use the same code with in my actual application, I see "OutOfMemoryError".Below is the code snippet. I have been spending a lot of time on this and any help would be highly appreciated.

Scan Activity:

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == Activity.RESULT_OK) {
        Uri imageUri = getPickImageResultUri(data);

//441

       ((CropImageView) findViewById(R.id.CropImageView)).setImageUri(imageUri);
    }
}

activity_scan.xml: Posting the essential code only

<com.theartofdev.edmodo.cropper.CropImageView
android:id="@+id/CropImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/layout1"
android:layout_gravity="center_horizontal"
android:background="@color/white"
android:layout_marginTop="2dp"
/>

<ImageView
android:id="@+id/croppedImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_below="@+id/CropImageView"
android:background="@color/white"/>

Log cat says:

java.lang.OutOfMemoryError
        at android.graphics.Bitmap.nativeCreate(Native Method)
        at android.graphics.Bitmap.createBitmap(Bitmap.java:928)
        at android.graphics.Bitmap.createBitmap(Bitmap.java:901)
        at android.graphics.Bitmap.createBitmap(Bitmap.java:833)
        at com.theartofdev.edmodo.cropper.util.ImageViewUtil.rotateBitmap(ImageViewUtil.java:239)
        at com.theartofdev.edmodo.cropper.util.ImageViewUtil.rotateBitmapByExif(ImageViewUtil.java:124)
        at com.theartofdev.edmodo.cropper.util.ImageViewUtil.rotateBitmapByExif(ImageViewUtil.java:97)
        at com.theartofdev.edmodo.cropper.CropImageView.setImageUri(CropImageView.java:295)
        at com.example.simcoeappco.Clync.ScanActivity.onActivityResult(ScanActivity.java:441)
        at android.app.Activity.dispatchActivityResult(Activity.java:5643)
        at android.app.ActivityThread.deliverResults(ActivityThread.java:3576)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:3623)
        at android.app.ActivityThread.access$1400(ActivityThread.java:169)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1328)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:146)
        at android.app.ActivityThread.main(ActivityThread.java:5487)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
        at dalvik.system.NativeStart.main(Native Method)

CropImageView:

  public void setImageUri(Uri uri) {
    if (uri != null) {

        DisplayMetrics metrics = getResources().getDisplayMetrics();
        double densityAdj = metrics.density > 1 ? 1 / metrics.density : 1;

        int width = (int) (metrics.widthPixels * densityAdj);
        int height = (int) (metrics.heightPixels * densityAdj);
        ImageViewUtil.DecodeBitmapResult decodeResult =
                ImageViewUtil.decodeSampledBitmap(getContext(), uri, width, height);

//295

        ImageViewUtil.RotateBitmapResult rotateResult =
                ImageViewUtil.rotateBitmapByExif(getContext(), decodeResult.bitmap, uri);

        setImageBitmap(rotateResult.bitmap);

        mLoadedImageUri = uri;
        mLoadedSampleSize = decodeResult.sampleSize;
        mDegreesRotated = rotateResult.degrees;
    }
}

ImageViewUtil.java

  public static RotateBitmapResult rotateBitmapByExif(Context context, Bitmap bitmap, Uri uri) {
    try {
        File file = getFileFromUri(context, uri);
        if (file.exists()) {
            ExifInterface ei = new ExifInterface(file.getAbsolutePath());

//97

            return rotateBitmapByExif(bitmap, ei);
        }
    } catch (Exception ignored) {
    }
    return new RotateBitmapResult(bitmap, 0);
}

ImageViewUtil.java

 public static RotateBitmapResult rotateBitmapByExif(Bitmap bitmap, ExifInterface exif) {
    int degrees = 0;
    int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
    switch (orientation) {
        case ExifInterface.ORIENTATION_ROTATE_90:
            degrees = 90;
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            degrees = 180;
            break;
        case ExifInterface.ORIENTATION_ROTATE_270:
            degrees = 270;
            break;
    }
    if (degrees > 0) {

//124

        bitmap = rotateBitmap(bitmap, degrees);
    }
    return new RotateBitmapResult(bitmap, degrees);
}

ImageViewUtil.java

  public static Bitmap rotateBitmap(Bitmap bitmap, int degrees) {
    Matrix matrix = new Matrix();
    matrix.setRotate(degrees);

//239

   Bitmap newBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false);
    bitmap.recycle();
    return newBitmap;
}