recover if attachImage can’t generate scalled down version of image. fixes #3773
This commit is contained in:
		
							parent
							
								
									1853242c66
								
							
						
					
					
						commit
						7e2d87f39c
					
				@ -704,7 +704,7 @@ 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 {
 | 
					    private void copyImageToPrivateStorage(File file, Uri image, int sampleSize) throws FileCopyException, NotAnImageFileException {
 | 
				
			||||||
        file.getParentFile().mkdirs();
 | 
					        file.getParentFile().mkdirs();
 | 
				
			||||||
        InputStream is = null;
 | 
					        InputStream is = null;
 | 
				
			||||||
        OutputStream os = null;
 | 
					        OutputStream os = null;
 | 
				
			||||||
@ -724,7 +724,7 @@ 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 FileCopyException(R.string.error_not_an_image_file);
 | 
					                throw new NotAnImageFileException();
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            Bitmap scaledBitmap = resize(originalBitmap, Config.IMAGE_SIZE);
 | 
					            Bitmap scaledBitmap = resize(originalBitmap, Config.IMAGE_SIZE);
 | 
				
			||||||
            int rotation = getRotation(image);
 | 
					            int rotation = getRotation(image);
 | 
				
			||||||
@ -763,12 +763,12 @@ public class FileBackend {
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public void copyImageToPrivateStorage(File file, Uri image) throws FileCopyException {
 | 
					    public void copyImageToPrivateStorage(File file, Uri image) throws FileCopyException, NotAnImageFileException {
 | 
				
			||||||
        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 {
 | 
					    public void copyImageToPrivateStorage(Message message, Uri image) throws FileCopyException, NotAnImageFileException {
 | 
				
			||||||
        switch (Config.IMAGE_FORMAT) {
 | 
					        switch (Config.IMAGE_FORMAT) {
 | 
				
			||||||
            case JPEG:
 | 
					            case JPEG:
 | 
				
			||||||
                message.setRelativeFilePath(message.getUuid() + ".jpg");
 | 
					                message.setRelativeFilePath(message.getUuid() + ".jpg");
 | 
				
			||||||
@ -1420,11 +1420,14 @@ public class FileBackend {
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public class FileCopyException extends Exception {
 | 
					    public static class NotAnImageFileException extends Exception {
 | 
				
			||||||
        private static final long serialVersionUID = -1010013599132881427L;
 | 
					
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public static class FileCopyException extends Exception {
 | 
				
			||||||
        private int resId;
 | 
					        private int resId;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        public FileCopyException(int resId) {
 | 
					        private FileCopyException(int resId) {
 | 
				
			||||||
            this.resId = resId;
 | 
					            this.resId = resId;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -567,6 +567,13 @@ public class XmppConnectionService extends Service {
 | 
				
			|||||||
        mFileAddingExecutor.execute(() -> {
 | 
					        mFileAddingExecutor.execute(() -> {
 | 
				
			||||||
            try {
 | 
					            try {
 | 
				
			||||||
                getFileBackend().copyImageToPrivateStorage(message, uri);
 | 
					                getFileBackend().copyImageToPrivateStorage(message, uri);
 | 
				
			||||||
 | 
					            } catch (FileBackend.NotAnImageFileException e) {
 | 
				
			||||||
 | 
					                attachFileToConversation(conversation, uri, mimeType, callback);
 | 
				
			||||||
 | 
					                return;
 | 
				
			||||||
 | 
					            } catch (final FileBackend.FileCopyException e) {
 | 
				
			||||||
 | 
					                callback.error(e.getResId(), message);
 | 
				
			||||||
 | 
					                return;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
            if (conversation.getNextEncryption() == Message.ENCRYPTION_PGP) {
 | 
					            if (conversation.getNextEncryption() == Message.ENCRYPTION_PGP) {
 | 
				
			||||||
                final PgpEngine pgpEngine = getPgpEngine();
 | 
					                final PgpEngine pgpEngine = getPgpEngine();
 | 
				
			||||||
                if (pgpEngine != null) {
 | 
					                if (pgpEngine != null) {
 | 
				
			||||||
@ -578,9 +585,6 @@ public class XmppConnectionService extends Service {
 | 
				
			|||||||
                sendMessage(message);
 | 
					                sendMessage(message);
 | 
				
			||||||
                callback.success(message);
 | 
					                callback.success(message);
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            } catch (final FileBackend.FileCopyException e) {
 | 
					 | 
				
			||||||
                callback.error(e.getResId(), message);
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -35,7 +35,6 @@ import android.content.Intent;
 | 
				
			|||||||
import android.net.Uri;
 | 
					import android.net.Uri;
 | 
				
			||||||
import android.os.Parcel;
 | 
					import android.os.Parcel;
 | 
				
			||||||
import android.os.Parcelable;
 | 
					import android.os.Parcelable;
 | 
				
			||||||
import android.util.Log;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
import java.io.File;
 | 
					import java.io.File;
 | 
				
			||||||
import java.util.ArrayList;
 | 
					import java.util.ArrayList;
 | 
				
			||||||
@ -43,7 +42,6 @@ import java.util.Collections;
 | 
				
			|||||||
import java.util.List;
 | 
					import java.util.List;
 | 
				
			||||||
import java.util.UUID;
 | 
					import java.util.UUID;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import eu.siacs.conversations.Config;
 | 
					 | 
				
			||||||
import eu.siacs.conversations.utils.Compatibility;
 | 
					import eu.siacs.conversations.utils.Compatibility;
 | 
				
			||||||
import eu.siacs.conversations.utils.MimeUtils;
 | 
					import eu.siacs.conversations.utils.MimeUtils;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -130,13 +128,13 @@ public class Attachment implements Parcelable {
 | 
				
			|||||||
        List<Attachment> attachments = new ArrayList<>();
 | 
					        List<Attachment> attachments = new ArrayList<>();
 | 
				
			||||||
        for (Uri uri : uris) {
 | 
					        for (Uri uri : uris) {
 | 
				
			||||||
            final String mime = MimeUtils.guessMimeTypeFromUri(context, uri);
 | 
					            final String mime = MimeUtils.guessMimeTypeFromUri(context, uri);
 | 
				
			||||||
            attachments.add(new Attachment(uri, mime != null && mime.startsWith("image/") ? Type.IMAGE : Type.FILE,mime));
 | 
					            attachments.add(new Attachment(uri, mime != null && isImage(mime) ? Type.IMAGE : Type.FILE, mime));
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        return attachments;
 | 
					        return attachments;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public static Attachment of(UUID uuid, final File file, String mime) {
 | 
					    public static Attachment of(UUID uuid, final File file, String mime) {
 | 
				
			||||||
        return new Attachment(uuid, Uri.fromFile(file),mime != null && (mime.startsWith("image/") || mime.startsWith("video/")) ? Type.IMAGE : Type.FILE, mime);
 | 
					        return new Attachment(uuid, Uri.fromFile(file), mime != null && (isImage(mime) || mime.startsWith("video/")) ? Type.IMAGE : Type.FILE, mime);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public static List<Attachment> extractAttachments(final Context context, final Intent intent, Type type) {
 | 
					    public static List<Attachment> extractAttachments(final Context context, final Intent intent, Type type) {
 | 
				
			||||||
@ -151,9 +149,7 @@ public class Attachment implements Parcelable {
 | 
				
			|||||||
            if (clipData != null) {
 | 
					            if (clipData != null) {
 | 
				
			||||||
                for (int i = 0; i < clipData.getItemCount(); ++i) {
 | 
					                for (int i = 0; i < clipData.getItemCount(); ++i) {
 | 
				
			||||||
                    final Uri uri = clipData.getItemAt(i).getUri();
 | 
					                    final Uri uri = clipData.getItemAt(i).getUri();
 | 
				
			||||||
                    Log.d(Config.LOGTAG,"uri="+uri+" contentType="+contentType);
 | 
					 | 
				
			||||||
                    final String mime = MimeUtils.guessMimeTypeFromUriAndMime(context, uri, contentType);
 | 
					                    final String mime = MimeUtils.guessMimeTypeFromUriAndMime(context, uri, contentType);
 | 
				
			||||||
                    Log.d(Config.LOGTAG,"mime="+mime);
 | 
					 | 
				
			||||||
                    uris.add(new Attachment(uri, type, mime));
 | 
					                    uris.add(new Attachment(uri, type, mime));
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
@ -170,7 +166,7 @@ public class Attachment implements Parcelable {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    private static boolean renderFileThumbnail(final String mime) {
 | 
					    private static boolean renderFileThumbnail(final String mime) {
 | 
				
			||||||
        return mime.startsWith("video/")
 | 
					        return mime.startsWith("video/")
 | 
				
			||||||
                || mime.startsWith("image/")
 | 
					                || isImage(mime)
 | 
				
			||||||
                || (Compatibility.runsTwentyOne() && "application/pdf".equals(mime));
 | 
					                || (Compatibility.runsTwentyOne() && "application/pdf".equals(mime));
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -181,4 +177,8 @@ public class Attachment implements Parcelable {
 | 
				
			|||||||
    public UUID getUuid() {
 | 
					    public UUID getUuid() {
 | 
				
			||||||
        return uuid;
 | 
					        return uuid;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private static boolean isImage(final String mime) {
 | 
				
			||||||
 | 
					        return mime.startsWith("image/") && !mime.equals("image/svg+xml");
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user