Resize replied images

pull/157/head
M66B 6 years ago
parent 28fd55a639
commit 18f0e29437

@ -1839,6 +1839,8 @@ public class FragmentCompose extends FragmentBase {
} }
} }
db.attachment().setDownloaded(attachment.id, size);
if ("eu.faircode.email".equals(uri.getAuthority())) { if ("eu.faircode.email".equals(uri.getAuthority())) {
// content://eu.faircode.email/temporary/nnn.jpg // content://eu.faircode.email/temporary/nnn.jpg
File tmp = new File(context.getFilesDir(), uri.getPath()); File tmp = new File(context.getFilesDir(), uri.getPath());
@ -1848,63 +1850,68 @@ public class FragmentCompose extends FragmentBase {
} else } else
Log.i("Authority=" + uri.getAuthority()); Log.i("Authority=" + uri.getAuthority());
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); resizeAttachment(context, attachment);
boolean autoresize = prefs.getBoolean("autoresize", true);
} catch (IOException ex) {
// Reset progress on failure
Log.e(ex);
db.attachment().setError(attachment.id, Helper.formatThrowable(ex, false));
throw ex;
}
return attachment;
}
if (autoresize && file.exists() /* upload cancelled */ && private static void resizeAttachment(Context context, EntityAttachment attachment) throws IOException {
("image/jpeg".equals(attachment.type) || "image/png".equals(attachment.type))) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
BitmapFactory.Options options = new BitmapFactory.Options(); boolean autoresize = prefs.getBoolean("autoresize", true);
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(file.getAbsolutePath(), options);
int resize = prefs.getInt("resize", REDUCED_IMAGE_SIZE); File file = attachment.getFile(context);
int factor = 1; if (autoresize && file.exists() /* upload cancelled */ &&
while (options.outWidth / factor > resize || ("image/jpeg".equals(attachment.type) || "image/png".equals(attachment.type))) {
options.outHeight / factor > resize) BitmapFactory.Options options = new BitmapFactory.Options();
factor *= 2; options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(file.getAbsolutePath(), options);
Matrix rotation = ("image/jpeg".equals(attachment.type) ? Helper.getImageRotation(file) : null); int resize = prefs.getInt("resize", REDUCED_IMAGE_SIZE);
Log.i("Image type=" + attachment.type + " rotation=" + rotation);
if (factor > 1 || rotation != null) { int factor = 1;
options.inJustDecodeBounds = false; while (options.outWidth / factor > resize ||
options.inSampleSize = factor; options.outHeight / factor > resize)
factor *= 2;
Bitmap scaled = BitmapFactory.decodeFile(file.getAbsolutePath(), options); Matrix rotation = ("image/jpeg".equals(attachment.type) ? Helper.getImageRotation(file) : null);
if (scaled != null) { Log.i("Image type=" + attachment.type + " rotation=" + rotation);
Log.i("Image target size=" + scaled.getWidth() + "x" + scaled.getHeight() + " rotation=" + rotation);
if (rotation != null) { if (factor > 1 || rotation != null) {
Bitmap rotated = Bitmap.createBitmap(scaled, 0, 0, scaled.getWidth(), scaled.getHeight(), rotation, true); options.inJustDecodeBounds = false;
scaled.recycle(); options.inSampleSize = factor;
scaled = rotated;
}
try (OutputStream out = new BufferedOutputStream(new FileOutputStream(file))) { Bitmap scaled = BitmapFactory.decodeFile(file.getAbsolutePath(), options);
scaled.compress("image/jpeg".equals(attachment.type) if (scaled != null) {
? Bitmap.CompressFormat.JPEG Log.i("Image target size=" + scaled.getWidth() + "x" + scaled.getHeight() + " rotation=" + rotation);
: Bitmap.CompressFormat.PNG,
REDUCED_IMAGE_QUALITY, out);
} finally {
scaled.recycle();
}
size = file.length(); if (rotation != null) {
Bitmap rotated = Bitmap.createBitmap(scaled, 0, 0, scaled.getWidth(), scaled.getHeight(), rotation, true);
scaled.recycle();
scaled = rotated;
} }
}
}
db.attachment().setDownloaded(attachment.id, size); try (OutputStream out = new BufferedOutputStream(new FileOutputStream(file))) {
scaled.compress("image/jpeg".equals(attachment.type)
? Bitmap.CompressFormat.JPEG
: Bitmap.CompressFormat.PNG,
REDUCED_IMAGE_QUALITY, out);
} finally {
scaled.recycle();
}
} catch (IOException ex) { DB db = DB.getInstance(context);
// Reset progress on failure db.attachment().setDownloaded(attachment.id, file.length());
Log.e(ex); }
db.attachment().setError(attachment.id, Helper.formatThrowable(ex, false)); }
throw ex;
} }
return attachment;
} }
private SimpleTask<EntityMessage> draftLoader = new SimpleTask<EntityMessage>() { private SimpleTask<EntityMessage> draftLoader = new SimpleTask<EntityMessage>() {
@ -2177,6 +2184,9 @@ public class FragmentCompose extends FragmentBase {
File target = attachment.getFile(context); File target = attachment.getFile(context);
Helper.copy(source, target); Helper.copy(source, target);
if (!"forward".equals(action))
resizeAttachment(context, attachment);
} else } else
args.putBoolean("incomplete", true); args.putBoolean("incomplete", true);
} }

Loading…
Cancel
Save