diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/activity/loader/AttachmentContentLoader.java b/app/ui/legacy/src/main/java/com/fsck/k9/activity/loader/AttachmentContentLoader.java index 6a420c812..19ceff5b6 100644 --- a/app/ui/legacy/src/main/java/com/fsck/k9/activity/loader/AttachmentContentLoader.java +++ b/app/ui/legacy/src/main/java/com/fsck/k9/activity/loader/AttachmentContentLoader.java @@ -3,7 +3,6 @@ package com.fsck.k9.activity.loader; import java.io.File; import java.io.FileOutputStream; -import java.io.IOException; import java.io.InputStream; import android.content.ContentResolver; @@ -88,7 +87,7 @@ public class AttachmentContentLoader extends AsyncTaskLoader { cachedResultAttachment = sourceAttachment.deriveWithLoadComplete(file.getAbsolutePath()); return cachedResultAttachment; - } catch (IOException e) { + } catch (Exception e) { Timber.e(e, "Error saving attachment!"); } diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/activity/loader/AttachmentInfoLoader.java b/app/ui/legacy/src/main/java/com/fsck/k9/activity/loader/AttachmentInfoLoader.java index 6750dd52b..9bbf2e61d 100644 --- a/app/ui/legacy/src/main/java/com/fsck/k9/activity/loader/AttachmentInfoLoader.java +++ b/app/ui/legacy/src/main/java/com/fsck/k9/activity/loader/AttachmentInfoLoader.java @@ -46,63 +46,70 @@ public class AttachmentInfoLoader extends AsyncTaskLoader { @Override public Attachment loadInBackground() { - Uri uri = sourceAttachment.uri; - String contentType = sourceAttachment.contentType; + try { + Uri uri = sourceAttachment.uri; + String contentType = sourceAttachment.contentType; - long size = -1; - String name = null; + long size = -1; + String name = null; - ContentResolver contentResolver = getContext().getContentResolver(); + ContentResolver contentResolver = getContext().getContentResolver(); - Cursor metadataCursor = contentResolver.query( - uri, - new String[] { OpenableColumns.DISPLAY_NAME, OpenableColumns.SIZE }, - null, - null, - null); + Cursor metadataCursor = contentResolver.query( + uri, + new String[] { OpenableColumns.DISPLAY_NAME, OpenableColumns.SIZE }, + null, + null, + null); - if (metadataCursor != null) { - try { - if (metadataCursor.moveToFirst()) { - name = metadataCursor.getString(0); - size = metadataCursor.getInt(1); + if (metadataCursor != null) { + try { + if (metadataCursor.moveToFirst()) { + name = metadataCursor.getString(0); + size = metadataCursor.getInt(1); + } + } finally { + metadataCursor.close(); } - } finally { - metadataCursor.close(); } - } - if (name == null) { - name = uri.getLastPathSegment(); - } + if (name == null) { + name = uri.getLastPathSegment(); + } - String usableContentType = contentResolver.getType(uri); - if (usableContentType == null && contentType != null && contentType.indexOf('*') != -1) { - usableContentType = contentType; - } + String usableContentType = contentResolver.getType(uri); + if (usableContentType == null && contentType != null && contentType.indexOf('*') != -1) { + usableContentType = contentType; + } - if (usableContentType == null) { - usableContentType = MimeTypeUtil.getMimeTypeByExtension(name); - } + if (usableContentType == null) { + usableContentType = MimeTypeUtil.getMimeTypeByExtension(name); + } - if (!sourceAttachment.allowMessageType && MimeUtility.isMessageType(usableContentType)) { - usableContentType = MimeTypeUtil.DEFAULT_ATTACHMENT_MIME_TYPE; - } + if (!sourceAttachment.allowMessageType && MimeUtility.isMessageType(usableContentType)) { + usableContentType = MimeTypeUtil.DEFAULT_ATTACHMENT_MIME_TYPE; + } - if (size <= 0) { - String uriString = uri.toString(); - if (uriString.startsWith("file://")) { - File f = new File(uriString.substring("file://".length())); - size = f.length(); + if (size <= 0) { + String uriString = uri.toString(); + if (uriString.startsWith("file://")) { + File f = new File(uriString.substring("file://".length())); + size = f.length(); + } else { + Timber.v("Not a file: %s", uriString); + } } else { - Timber.v("Not a file: %s", uriString); + Timber.v("old attachment.size: %d", size); } - } else { - Timber.v("old attachment.size: %d", size); - } - Timber.v("new attachment.size: %d", size); + Timber.v("new attachment.size: %d", size); - cachedResultAttachment = sourceAttachment.deriveWithMetadataLoaded(usableContentType, name, size); - return cachedResultAttachment; + cachedResultAttachment = sourceAttachment.deriveWithMetadataLoaded(usableContentType, name, size); + return cachedResultAttachment; + } catch (Exception e) { + Timber.e(e, "Error getting attachment meta data"); + + cachedResultAttachment = sourceAttachment.deriveWithLoadCancelled(); + return cachedResultAttachment; + } } }