|
|
@ -5198,102 +5198,107 @@ public class FragmentCompose extends FragmentBase {
|
|
|
|
|
|
|
|
|
|
|
|
private static void resizeAttachment(Context context, EntityAttachment attachment, int resize) throws IOException {
|
|
|
|
private static void resizeAttachment(Context context, EntityAttachment attachment, int resize) throws IOException {
|
|
|
|
File file = attachment.getFile(context);
|
|
|
|
File file = attachment.getFile(context);
|
|
|
|
if (file.exists() /* upload cancelled */ &&
|
|
|
|
if (!file.exists()) // Upload cancelled;
|
|
|
|
("image/jpg".equals(attachment.type) ||
|
|
|
|
return;
|
|
|
|
"image/jpeg".equals(attachment.type) ||
|
|
|
|
|
|
|
|
"image/png".equals(attachment.type) ||
|
|
|
|
|
|
|
|
"image/webp".equals(attachment.type))) {
|
|
|
|
|
|
|
|
ExifInterface exifSaved;
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
exifSaved = new ExifInterface(file);
|
|
|
|
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
|
|
|
|
Log.w(ex);
|
|
|
|
|
|
|
|
exifSaved = null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
BitmapFactory.Options options = new BitmapFactory.Options();
|
|
|
|
|
|
|
|
options.inJustDecodeBounds = true;
|
|
|
|
|
|
|
|
BitmapFactory.decodeFile(file.getAbsolutePath(), options);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int factor = 1;
|
|
|
|
|
|
|
|
while (options.outWidth / factor > resize ||
|
|
|
|
|
|
|
|
options.outHeight / factor > resize)
|
|
|
|
|
|
|
|
factor *= 2;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Matrix rotation = ("image/jpeg".equals(attachment.type) ? ImageHelper.getImageRotation(file) : null);
|
|
|
|
|
|
|
|
Log.i("Image type=" + attachment.type + " rotation=" + rotation);
|
|
|
|
|
|
|
|
if (factor > 1 || rotation != null) {
|
|
|
|
|
|
|
|
options.inJustDecodeBounds = false;
|
|
|
|
|
|
|
|
options.inSampleSize = factor;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Log.i("Image target size=" + resize + " factor=" + factor + " source=" + options.outWidth + "x" + options.outHeight);
|
|
|
|
|
|
|
|
Bitmap resized = BitmapFactory.decodeFile(file.getAbsolutePath(), options);
|
|
|
|
|
|
|
|
if (resized != null) {
|
|
|
|
|
|
|
|
Log.i("Image result size=" + resized.getWidth() + "x" + resized.getHeight() + " rotation=" + rotation);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (rotation != null) {
|
|
|
|
|
|
|
|
Bitmap rotated = Bitmap.createBitmap(resized, 0, 0, resized.getWidth(), resized.getHeight(), rotation, true);
|
|
|
|
|
|
|
|
resized.recycle();
|
|
|
|
|
|
|
|
resized = rotated;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Bitmap.CompressFormat format;
|
|
|
|
if (!"image/jpg".equals(attachment.type) &&
|
|
|
|
if ("image/jpg".equals(attachment.type) ||
|
|
|
|
!"image/jpeg".equals(attachment.type) &&
|
|
|
|
"image/jpeg".equals(attachment.type))
|
|
|
|
!"image/png".equals(attachment.type) &&
|
|
|
|
format = Bitmap.CompressFormat.JPEG;
|
|
|
|
!"image/webp".equals(attachment.type)) {
|
|
|
|
else if ("image/png".equals(attachment.type))
|
|
|
|
Log.i("Skipping resize of type=" + attachment.type);
|
|
|
|
format = Bitmap.CompressFormat.PNG;
|
|
|
|
return;
|
|
|
|
else if ("image/webp".equals(attachment.type))
|
|
|
|
}
|
|
|
|
format = Bitmap.CompressFormat.WEBP;
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
throw new IllegalArgumentException("Invalid format type=" + attachment.type);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
File tmp = new File(file.getAbsolutePath() + ".tmp");
|
|
|
|
ExifInterface exifSaved;
|
|
|
|
try (OutputStream out = new BufferedOutputStream(new FileOutputStream(tmp))) {
|
|
|
|
try {
|
|
|
|
if (!resized.compress(format, REDUCED_IMAGE_QUALITY, out))
|
|
|
|
exifSaved = new ExifInterface(file);
|
|
|
|
throw new IOException("compress");
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.w(ex);
|
|
|
|
Log.w(ex);
|
|
|
|
exifSaved = null;
|
|
|
|
Helper.secureDelete(tmp);
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
resized.recycle();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (tmp.exists() && tmp.length() > 0) {
|
|
|
|
BitmapFactory.Options options = new BitmapFactory.Options();
|
|
|
|
Helper.secureDelete(file);
|
|
|
|
options.inJustDecodeBounds = true;
|
|
|
|
tmp.renameTo(file);
|
|
|
|
BitmapFactory.decodeFile(file.getAbsolutePath(), options);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int factor = 1;
|
|
|
|
|
|
|
|
while (options.outWidth / factor > resize ||
|
|
|
|
|
|
|
|
options.outHeight / factor > resize)
|
|
|
|
|
|
|
|
factor *= 2;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Matrix rotation = ("image/jpeg".equals(attachment.type) ? ImageHelper.getImageRotation(file) : null);
|
|
|
|
|
|
|
|
Log.i("Image type=" + attachment.type + " rotation=" + rotation);
|
|
|
|
|
|
|
|
if (factor > 1 || rotation != null) {
|
|
|
|
|
|
|
|
options.inJustDecodeBounds = false;
|
|
|
|
|
|
|
|
options.inSampleSize = factor;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Log.i("Image target size=" + resize + " factor=" + factor + " source=" + options.outWidth + "x" + options.outHeight);
|
|
|
|
|
|
|
|
Bitmap resized = BitmapFactory.decodeFile(file.getAbsolutePath(), options);
|
|
|
|
|
|
|
|
if (resized != null) {
|
|
|
|
|
|
|
|
Log.i("Image result size=" + resized.getWidth() + "x" + resized.getHeight() + " rotation=" + rotation);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (rotation != null) {
|
|
|
|
|
|
|
|
Bitmap rotated = Bitmap.createBitmap(resized, 0, 0, resized.getWidth(), resized.getHeight(), rotation, true);
|
|
|
|
|
|
|
|
resized.recycle();
|
|
|
|
|
|
|
|
resized = rotated;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
Bitmap.CompressFormat format;
|
|
|
|
db.attachment().setDownloaded(attachment.id, file.length());
|
|
|
|
if ("image/jpg".equals(attachment.type) ||
|
|
|
|
|
|
|
|
"image/jpeg".equals(attachment.type))
|
|
|
|
|
|
|
|
format = Bitmap.CompressFormat.JPEG;
|
|
|
|
|
|
|
|
else if ("image/png".equals(attachment.type))
|
|
|
|
|
|
|
|
format = Bitmap.CompressFormat.PNG;
|
|
|
|
|
|
|
|
else if ("image/webp".equals(attachment.type))
|
|
|
|
|
|
|
|
format = Bitmap.CompressFormat.WEBP;
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
throw new IllegalArgumentException("Invalid format type=" + attachment.type);
|
|
|
|
|
|
|
|
|
|
|
|
if (exifSaved != null)
|
|
|
|
File tmp = new File(file.getAbsolutePath() + ".tmp");
|
|
|
|
try {
|
|
|
|
try (OutputStream out = new BufferedOutputStream(new FileOutputStream(tmp))) {
|
|
|
|
ExifInterface exif = new ExifInterface(file);
|
|
|
|
if (!resized.compress(format, REDUCED_IMAGE_QUALITY, out))
|
|
|
|
|
|
|
|
throw new IOException("compress");
|
|
|
|
// Preserve time
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
if (exifSaved.hasAttribute(ExifInterface.TAG_DATETIME_ORIGINAL))
|
|
|
|
Log.w(ex);
|
|
|
|
exif.setAttribute(ExifInterface.TAG_DATETIME_ORIGINAL,
|
|
|
|
Helper.secureDelete(tmp);
|
|
|
|
exifSaved.getAttribute(ExifInterface.TAG_DATETIME_ORIGINAL));
|
|
|
|
} finally {
|
|
|
|
if (exifSaved.hasAttribute(ExifInterface.TAG_GPS_DATESTAMP))
|
|
|
|
resized.recycle();
|
|
|
|
exif.setAttribute(ExifInterface.TAG_GPS_DATESTAMP,
|
|
|
|
}
|
|
|
|
exifSaved.getAttribute(ExifInterface.TAG_GPS_DATESTAMP));
|
|
|
|
|
|
|
|
|
|
|
|
if (tmp.exists() && tmp.length() > 0) {
|
|
|
|
// Preserve location
|
|
|
|
Helper.secureDelete(file);
|
|
|
|
double[] latlong = exifSaved.getLatLong();
|
|
|
|
tmp.renameTo(file);
|
|
|
|
if (latlong != null)
|
|
|
|
|
|
|
|
exif.setLatLong(latlong[0], latlong[1]);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Preserve altitude
|
|
|
|
|
|
|
|
if (exifSaved.hasAttribute(ExifInterface.TAG_GPS_ALTITUDE) &&
|
|
|
|
|
|
|
|
exifSaved.hasAttribute(ExifInterface.TAG_GPS_ALTITUDE_REF))
|
|
|
|
|
|
|
|
exif.setAltitude(exifSaved.getAltitude(0));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exif.saveAttributes();
|
|
|
|
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
|
|
|
|
Log.w(ex);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
|
|
|
|
db.attachment().setDownloaded(attachment.id, file.length());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (exifSaved != null)
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
ExifInterface exif = new ExifInterface(file);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Preserve time
|
|
|
|
|
|
|
|
if (exifSaved.hasAttribute(ExifInterface.TAG_DATETIME_ORIGINAL))
|
|
|
|
|
|
|
|
exif.setAttribute(ExifInterface.TAG_DATETIME_ORIGINAL,
|
|
|
|
|
|
|
|
exifSaved.getAttribute(ExifInterface.TAG_DATETIME_ORIGINAL));
|
|
|
|
|
|
|
|
if (exifSaved.hasAttribute(ExifInterface.TAG_GPS_DATESTAMP))
|
|
|
|
|
|
|
|
exif.setAttribute(ExifInterface.TAG_GPS_DATESTAMP,
|
|
|
|
|
|
|
|
exifSaved.getAttribute(ExifInterface.TAG_GPS_DATESTAMP));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Preserve location
|
|
|
|
|
|
|
|
double[] latlong = exifSaved.getLatLong();
|
|
|
|
|
|
|
|
if (latlong != null)
|
|
|
|
|
|
|
|
exif.setLatLong(latlong[0], latlong[1]);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Preserve altitude
|
|
|
|
|
|
|
|
if (exifSaved.hasAttribute(ExifInterface.TAG_GPS_ALTITUDE) &&
|
|
|
|
|
|
|
|
exifSaved.hasAttribute(ExifInterface.TAG_GPS_ALTITUDE_REF))
|
|
|
|
|
|
|
|
exif.setAltitude(exifSaved.getAltitude(0));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exif.saveAttributes();
|
|
|
|
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
|
|
|
|
Log.w(ex);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|