From 711711274e9f51b7f033767702cc9016aa0601f3 Mon Sep 17 00:00:00 2001 From: M M Arif Date: Fri, 12 Feb 2021 23:16:29 +0100 Subject: [PATCH] Fix canvas too large crash (#830) fix crash by scaling the image resolution Co-authored-by: M M Arif Reviewed-on: https://codeberg.org/gitnex/GitNex/pulls/830 Reviewed-by: 6543 <6543@noreply.codeberg.org> Co-Authored-By: M M Arif Co-Committed-By: M M Arif --- .../gitnex/activities/FileViewActivity.java | 8 ++--- .../java/org/mian/gitnex/helpers/Images.java | 32 +++++++++++++++++++ 2 files changed, 35 insertions(+), 5 deletions(-) create mode 100644 app/src/main/java/org/mian/gitnex/helpers/Images.java diff --git a/app/src/main/java/org/mian/gitnex/activities/FileViewActivity.java b/app/src/main/java/org/mian/gitnex/activities/FileViewActivity.java index 8905a840..e3dbaabb 100644 --- a/app/src/main/java/org/mian/gitnex/activities/FileViewActivity.java +++ b/app/src/main/java/org/mian/gitnex/activities/FileViewActivity.java @@ -2,10 +2,7 @@ package org.mian.gitnex.activities; import android.app.Activity; import android.content.Intent; -import android.graphics.BitmapFactory; import android.graphics.Typeface; -import android.graphics.drawable.BitmapDrawable; -import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Bundle; import android.text.method.ScrollingMovementMethod; @@ -38,6 +35,7 @@ import org.mian.gitnex.databinding.ActivityFileViewBinding; import org.mian.gitnex.fragments.BottomSheetFileViewerFragment; import org.mian.gitnex.helpers.AlertDialogs; import org.mian.gitnex.helpers.AppUtil; +import org.mian.gitnex.helpers.Images; import org.mian.gitnex.helpers.Markdown; import org.mian.gitnex.helpers.Toasty; import org.mian.gitnex.helpers.highlightjs.HighlightJsView; @@ -182,8 +180,8 @@ public class FileViewActivity extends BaseActivity implements BottomSheetFileVie imageView.setVisibility(View.VISIBLE); imageData = Base64.decode(response.body().getContent(), Base64.DEFAULT); - Drawable imageDrawable = new BitmapDrawable(getResources(), BitmapFactory.decodeByteArray(imageData, 0, imageData.length)); - imageView.setImageDrawable(imageDrawable); + + imageView.setImageBitmap(Images.scaleImage(imageData, 1920, 1920)); } else if(appUtil.sourceCodeExtension(fileExtension)) { // file is sourcecode diff --git a/app/src/main/java/org/mian/gitnex/helpers/Images.java b/app/src/main/java/org/mian/gitnex/helpers/Images.java new file mode 100644 index 00000000..84f62ae4 --- /dev/null +++ b/app/src/main/java/org/mian/gitnex/helpers/Images.java @@ -0,0 +1,32 @@ +package org.mian.gitnex.helpers; + +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; + +/** + * Author M M Arif + */ + +public class Images { + + public static Bitmap scaleImage(byte[] imageData, int maxSizeWidth, int maxSizeScaledWidth) { + + Bitmap scaledImage; + Bitmap image = BitmapFactory.decodeByteArray(imageData, 0, imageData.length); + int orgWidth = image.getWidth(); + int orgHeight = image.getHeight(); + + if(orgWidth > maxSizeWidth) { + + int aspectRatio = orgWidth / orgHeight; + int scaledHeight = maxSizeScaledWidth * aspectRatio; + scaledImage = Bitmap.createScaledBitmap(image, maxSizeScaledWidth, scaledHeight, false); + } + else { + + scaledImage = Bitmap.createScaledBitmap(image, orgWidth, orgHeight, false); + } + + return scaledImage; + } +}