Resize too big (bytes) images

This commit is contained in:
M66B 2021-12-11 16:37:56 +01:00
parent 69b19fd97d
commit 3e28f12de0
2 changed files with 25 additions and 2 deletions

View File

@ -42,6 +42,7 @@ import android.content.pm.ResolveInfo;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.BatteryManager;
import android.os.Build;
@ -1318,6 +1319,25 @@ public class Helper {
}
}
static int getBytesPerPixel(Bitmap.Config config) {
switch (config) {
case ALPHA_8:
return 1;
case RGB_565:
return 2;
case ARGB_4444:
return 4;
case ARGB_8888:
return 8;
case RGBA_F16:
return 8;
case HARDWARE:
return 0;
default:
return 8;
}
}
// Formatting
private static final DecimalFormat df = new DecimalFormat("@@");

View File

@ -89,6 +89,7 @@ class ImageHelper {
static final int DOWNLOAD_TIMEOUT = 15; // seconds
private static final int MAX_PROBE = 128 * 1024; // bytes
private static final int SLOW_CONNECTION = 2 * 1024; // Kbps
private static final int MAX_BITMAP_SIZE = 100 * 1024 * 1024; // RecordingCanvas.MAX_BITMAP_SIZE
// https://developer.android.com/guide/topics/media/media-formats#image-formats
static final List<String> IMAGE_TYPES = Collections.unmodifiableList(Arrays.asList(
@ -816,7 +817,8 @@ class ImageHelper {
@NonNull ImageDecoder.ImageInfo info,
@NonNull ImageDecoder.Source source) {
int factor = 1;
while (info.getSize().getWidth() / factor > scaleToPixels)
while (info.getSize().getWidth() / factor > scaleToPixels ||
info.getSize().getWidth() * info.getSize().getHeight() * 8 / factor > MAX_BITMAP_SIZE)
factor *= 2;
Log.i("Decode image (decoder) factor=" + factor);
@ -833,7 +835,8 @@ class ImageHelper {
BitmapFactory.decodeFile(file.getAbsolutePath(), options);
int factor = 1;
while (options.outWidth / factor > scaleToPixels)
while (options.outWidth / factor > scaleToPixels ||
options.outWidth * options.outHeight * 8 / factor > MAX_BITMAP_SIZE)
factor *= 2;
if (factor > 1) {