Use temporary file for resizing images

pull/160/head
M66B 5 years ago
parent b9411b82d6
commit f0456e1a41

@ -1903,25 +1903,29 @@ public class FragmentCompose extends FragmentBase {
options.inJustDecodeBounds = false; options.inJustDecodeBounds = false;
options.inSampleSize = factor; options.inSampleSize = factor;
Bitmap scaled = BitmapFactory.decodeFile(file.getAbsolutePath(), options); Bitmap resized = BitmapFactory.decodeFile(file.getAbsolutePath(), options);
if (scaled != null) { if (resized != null) {
Log.i("Image target size=" + scaled.getWidth() + "x" + scaled.getHeight() + " rotation=" + rotation); Log.i("Image target size=" + resized.getWidth() + "x" + resized.getHeight() + " rotation=" + rotation);
if (rotation != null) { if (rotation != null) {
Bitmap rotated = Bitmap.createBitmap(scaled, 0, 0, scaled.getWidth(), scaled.getHeight(), rotation, true); Bitmap rotated = Bitmap.createBitmap(resized, 0, 0, resized.getWidth(), resized.getHeight(), rotation, true);
scaled.recycle(); resized.recycle();
scaled = rotated; resized = rotated;
} }
try (OutputStream out = new FileOutputStream(file)) { File tmp = File.createTempFile(Long.toString(attachment.id), ".resized", context.getCacheDir());
scaled.compress("image/jpeg".equals(attachment.type) try (OutputStream out = new FileOutputStream(tmp)) {
resized.compress("image/jpeg".equals(attachment.type)
? Bitmap.CompressFormat.JPEG ? Bitmap.CompressFormat.JPEG
: Bitmap.CompressFormat.PNG, : Bitmap.CompressFormat.PNG,
REDUCED_IMAGE_QUALITY, out); REDUCED_IMAGE_QUALITY, out);
} finally { } finally {
scaled.recycle(); resized.recycle();
} }
file.delete();
tmp.renameTo(file);
DB db = DB.getInstance(context); DB db = DB.getInstance(context);
db.attachment().setDownloaded(attachment.id, file.length()); db.attachment().setDownloaded(attachment.id, file.length());
} }

Loading…
Cancel
Save