do not compress images with alpha channels

This commit is contained in:
Daniel Gultsch 2021-01-29 21:25:00 +01:00
parent 4a9dfb9567
commit 2155a50875
3 changed files with 24 additions and 11 deletions

View File

@ -704,8 +704,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 {
@ -724,7 +727,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");
} }
Bitmap scaledBitmap = resize(originalBitmap, Config.IMAGE_SIZE); Bitmap scaledBitmap = resize(originalBitmap, Config.IMAGE_SIZE);
int rotation = getRotation(image); int rotation = getRotation(image);
@ -763,12 +770,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 {
switch (Config.IMAGE_FORMAT) { switch (Config.IMAGE_FORMAT) {
case JPEG: case JPEG:
message.setRelativeFilePath(message.getUuid() + ".jpg"); message.setRelativeFilePath(message.getUuid() + ".jpg");
@ -829,11 +836,11 @@ public class FileBackend {
} else if (mime.startsWith("video/")) { } else if (mime.startsWith("video/")) {
thumbnail = getVideoPreview(file, size); thumbnail = getVideoPreview(file, size);
} else { } else {
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);
@ -1439,10 +1446,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 final int resId; private final int resId;

View File

@ -583,7 +583,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) {
@ -636,7 +637,7 @@ public class XmppConnectionService extends Service {
switch (action) { switch (action) {
case QuickConversationsService.SMS_RETRIEVED_ACTION: case QuickConversationsService.SMS_RETRIEVED_ACTION:
mQuickConversationsService.handleSmsReceived(intent); mQuickConversationsService.handleSmsReceived(intent);
break; break;
case ConnectivityManager.CONNECTIVITY_ACTION: case ConnectivityManager.CONNECTIVITY_ACTION:
if (hasInternetConnection()) { if (hasInternetConnection()) {
if (Config.POST_CONNECTIVITY_CHANGE_PING_INTERVAL > 0) { if (Config.POST_CONNECTIVITY_CHANGE_PING_INTERVAL > 0) {

View File

@ -131,6 +131,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) {