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;
}
private void copyImageToPrivateStorage(File file, Uri image, int sampleSize) throws FileCopyException, NotAnImageFileException {
file.getParentFile().mkdirs();
private void copyImageToPrivateStorage(File file, Uri image, int sampleSize) throws FileCopyException, ImageCompressionException {
final File parent = file.getParentFile();
if (parent.mkdirs()) {
Log.d(Config.LOGTAG,"created parent directory");
}
InputStream is = null;
OutputStream os = null;
try {
@ -724,7 +727,11 @@ public class FileBackend {
originalBitmap = BitmapFactory.decodeStream(is, null, options);
is.close();
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);
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());
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) {
case JPEG:
message.setRelativeFilePath(message.getUuid() + ".jpg");
@ -829,11 +836,11 @@ public class FileBackend {
} else if (mime.startsWith("video/")) {
thumbnail = getVideoPreview(file, size);
} else {
Bitmap fullsize = getFullSizeImagePreview(file, size);
if (fullsize == null) {
final Bitmap fullSize = getFullSizeImagePreview(file, size);
if (fullSize == null) {
throw new FileNotFoundException();
}
thumbnail = resize(fullsize, size);
thumbnail = resize(fullSize, size);
thumbnail = rotate(thumbnail, getRotation(file));
if (mime.equals("image/gif")) {
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 {
private final int resId;

View File

@ -583,7 +583,8 @@ public class XmppConnectionService extends Service {
mFileAddingExecutor.execute(() -> {
try {
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);
return;
} catch (final FileBackend.FileCopyException e) {
@ -636,7 +637,7 @@ public class XmppConnectionService extends Service {
switch (action) {
case QuickConversationsService.SMS_RETRIEVED_ACTION:
mQuickConversationsService.handleSmsReceived(intent);
break;
break;
case ConnectivityManager.CONNECTIVITY_ACTION:
if (hasInternetConnection()) {
if (Config.POST_CONNECTIVITY_CHANGE_PING_INTERVAL > 0) {

View File

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