do not compress images with alpha channels

(cherry picked from commit 2155a50875)
This commit is contained in:
Daniel Gultsch 2021-01-29 21:25:00 +01:00 committed by Christian Schneppe
parent 8cf631a9cf
commit 4a454b74ac
3 changed files with 23 additions and 10 deletions

View file

@ -525,8 +525,11 @@ public class FileBackend {
return pos > 0 ? filename.substring(pos + 1) : null; return pos > 0 ? filename.substring(pos + 1) : null;
} }
private void copyImageToPrivateStorage(File file, Uri image, int sampleSize) throws FileCopyException, NotAnImageFileException { private void copyImageToPrivateStorage(File file, Uri image, int sampleSize) throws FileCopyException, ImageCompressionException {
file.getParentFile().mkdirs(); final File parent = file.getParentFile();
if (parent.mkdirs()) {
Log.d(Config.LOGTAG,"created parent directory");
}
InputStream is = null; InputStream is = null;
OutputStream os = null; OutputStream os = null;
try { try {
@ -545,7 +548,11 @@ public class FileBackend {
originalBitmap = BitmapFactory.decodeStream(is, null, options); originalBitmap = BitmapFactory.decodeStream(is, null, options);
is.close(); is.close();
if (originalBitmap == null) { if (originalBitmap == null) {
throw new NotAnImageFileException(); throw new ImageCompressionException("Source file was not an image");
}
if (hasAlpha(originalBitmap)) {
originalBitmap.recycle();
throw new ImageCompressionException("Source file had alpha channel");
} }
int size; int size;
if (mXmppConnectionService.getCompressImageResolutionPreference() == 0) { if (mXmppConnectionService.getCompressImageResolutionPreference() == 0) {
@ -591,12 +598,12 @@ public class FileBackend {
} }
} }
public void copyImageToPrivateStorage(File file, Uri image) throws FileCopyException, NotAnImageFileException { public void copyImageToPrivateStorage(File file, Uri image) throws FileCopyException, ImageCompressionException {
Log.d(Config.LOGTAG, "copy image (" + image.toString() + ") to private storage " + file.getAbsolutePath()); Log.d(Config.LOGTAG, "copy image (" + image.toString() + ") to private storage " + file.getAbsolutePath());
copyImageToPrivateStorage(file, image, 0); copyImageToPrivateStorage(file, image, 0);
} }
public void copyImageToPrivateStorage(Message message, Uri image) throws FileCopyException, NotAnImageFileException { public void copyImageToPrivateStorage(Message message, Uri image) throws FileCopyException, ImageCompressionException {
String filename = "Sent/" + fileDateFormat.format(new Date(message.getTimeSent())) + "_" + message.getUuid().substring(0, 4); String filename = "Sent/" + fileDateFormat.format(new Date(message.getTimeSent())) + "_" + message.getUuid().substring(0, 4);
switch (Config.IMAGE_FORMAT) { switch (Config.IMAGE_FORMAT) {
case JPEG: case JPEG:
@ -664,11 +671,11 @@ public class FileBackend {
} else if (mime.startsWith("video/")) { } else if (mime.startsWith("video/")) {
thumbnail = getVideoPreview(file, size); thumbnail = getVideoPreview(file, size);
} else if (mime.startsWith("image/")) { } else if (mime.startsWith("image/")) {
Bitmap fullsize = getFullsizeImagePreview(file, size); final Bitmap fullSize = getFullsizeImagePreview(file, size);
if (fullsize == null) { if (fullSize == null) {
throw new FileNotFoundException(); throw new FileNotFoundException();
} }
thumbnail = resize(fullsize, size); thumbnail = resize(fullSize, size);
thumbnail = rotate(thumbnail, getRotation(file)); thumbnail = rotate(thumbnail, getRotation(file));
if (mime.equals("image/gif")) { if (mime.equals("image/gif")) {
Bitmap withGifOverlay = thumbnail.copy(Bitmap.Config.ARGB_8888, true); Bitmap withGifOverlay = thumbnail.copy(Bitmap.Config.ARGB_8888, true);
@ -1534,10 +1541,14 @@ public class FileBackend {
} }
} }
public static class NotAnImageFileException extends Exception { public static class ImageCompressionException extends Exception {
ImageCompressionException(String message) {
super(message);
}
} }
public static class FileCopyException extends Exception { public static class FileCopyException extends Exception {
private int resId; private int resId;

View file

@ -603,7 +603,8 @@ public class XmppConnectionService extends Service {
mFileAddingExecutor.execute(() -> { mFileAddingExecutor.execute(() -> {
try { try {
getFileBackend().copyImageToPrivateStorage(message, uri); getFileBackend().copyImageToPrivateStorage(message, uri);
} catch (FileBackend.NotAnImageFileException e) { } catch (FileBackend.ImageCompressionException e) {
Log.d(Config.LOGTAG, "unable to compress image. fall back to file transfer", e);
attachFileToConversation(conversation, uri, mimeType, callback); attachFileToConversation(conversation, uri, mimeType, callback);
return; return;
} catch (final FileBackend.FileCopyException e) { } catch (final FileBackend.FileCopyException e) {

View file

@ -133,6 +133,7 @@ public class PublishProfilePictureActivity extends XmppActivity implements XmppC
@Override @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) { public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) { if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data); CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) { if (resultCode == RESULT_OK) {