Resize content images

This commit is contained in:
M66B 2020-05-04 13:17:27 +02:00
parent 1dce96a460
commit c8e830aff2
1 changed files with 31 additions and 5 deletions

View File

@ -317,11 +317,37 @@ class ImageHelper {
try {
Uri uri = Uri.parse(a.source);
Log.i("Loading image source=" + uri);
InputStream inputStream = context.getContentResolver().openInputStream(uri);
Drawable d = Drawable.createFromStream(inputStream, uri.toString());
if (d == null)
throw new FileNotFoundException(a.source);
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
DisplayMetrics dm = context.getResources().getDisplayMetrics();
BitmapFactory.Options options;
try (InputStream is = context.getContentResolver().openInputStream(uri)) {
options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(is, null, options);
}
int scaleToPixels = dm.widthPixels;
int factor = 1;
while (options.outWidth / factor > scaleToPixels)
factor *= 2;
Bitmap bm;
try (InputStream is = context.getContentResolver().openInputStream(uri)) {
if (factor > 1) {
options.inJustDecodeBounds = false;
options.inSampleSize = factor;
bm = BitmapFactory.decodeStream(is, null, options);
} else
bm = BitmapFactory.decodeStream(is);
if (bm == null)
throw new FileNotFoundException(a.source);
}
Drawable d = new BitmapDrawable(res, bm);
d.setBounds(0, 0, Math.round(bm.getWidth() * dm.density), Math.round(bm.getHeight() * dm.density));
if (view != null)
fitDrawable(d, a, view);
return d;