Fix canvas too large crash (#830)

fix crash by scaling the image resolution

Co-authored-by: M M Arif <mmarif@swatian.com>
Reviewed-on: https://codeberg.org/gitnex/GitNex/pulls/830
Reviewed-by: 6543 <6543@noreply.codeberg.org>
Co-Authored-By: M M Arif <mmarif@noreply.codeberg.org>
Co-Committed-By: M M Arif <mmarif@noreply.codeberg.org>
This commit is contained in:
M M Arif 2021-02-12 23:16:29 +01:00 committed by 6543
parent 44b8ad8d1c
commit 711711274e
2 changed files with 35 additions and 5 deletions

View File

@ -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

View File

@ -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;
}
}