diff --git a/src/com/fsck/k9/mail/store/LocalStore.java b/src/com/fsck/k9/mail/store/LocalStore.java index e90e5a64e..4c2ede1e5 100644 --- a/src/com/fsck/k9/mail/store/LocalStore.java +++ b/src/com/fsck/k9/mail/store/LocalStore.java @@ -988,49 +988,18 @@ public class LocalStore extends Store implements Serializable { } - public String getAttachmentType(final String attachmentId) throws UnavailableStorageException { - return database.execute(false, new DbCallback() { - @Override - public String doDbWork(final SQLiteDatabase db) throws WrappedException { - Cursor cursor = null; - try { - cursor = db.query( - "attachments", - new String[] { "mime_type", "name" }, - "id = ?", - new String[] { attachmentId }, - null, - null, - null); - cursor.moveToFirst(); - String type = cursor.getString(0); - String name = cursor.getString(1); - cursor.close(); - - if (MimeUtility.DEFAULT_ATTACHMENT_MIME_TYPE.equalsIgnoreCase(type)) { - type = MimeUtility.getMimeTypeByExtension(name); - } - return type; - } finally { - if (cursor != null) { - cursor.close(); - } - } - } - }); - } - public AttachmentInfo getAttachmentInfo(final String attachmentId) throws UnavailableStorageException { return database.execute(false, new DbCallback() { @Override public AttachmentInfo doDbWork(final SQLiteDatabase db) throws WrappedException { String name; + String type; int size; Cursor cursor = null; try { cursor = db.query( "attachments", - new String[] { "name", "size" }, + new String[] { "name", "size", "mime_type" }, "id = ?", new String[] { attachmentId }, null, @@ -1041,9 +1010,11 @@ public class LocalStore extends Store implements Serializable { } name = cursor.getString(0); size = cursor.getInt(1); + type = cursor.getString(2); final AttachmentInfo attachmentInfo = new AttachmentInfo(); attachmentInfo.name = name; attachmentInfo.size = size; + attachmentInfo.type = type; return attachmentInfo; } finally { if (cursor != null) { @@ -1057,6 +1028,7 @@ public class LocalStore extends Store implements Serializable { public static class AttachmentInfo { public String name; public int size; + public String type; } public void createFolders(final List foldersToCreate, final int visibleLimit) throws UnavailableStorageException { diff --git a/src/com/fsck/k9/provider/AttachmentProvider.java b/src/com/fsck/k9/provider/AttachmentProvider.java index 5deeecd61..2442278b5 100644 --- a/src/com/fsck/k9/provider/AttachmentProvider.java +++ b/src/com/fsck/k9/provider/AttachmentProvider.java @@ -105,8 +105,17 @@ public class AttachmentProvider extends ContentProvider { try { final LocalStore localStore = LocalStore.getLocalInstance(account, K9.app); - return MimeUtility.canonicalizeMimeType(localStore.getAttachmentType(id)); - + + AttachmentInfo attachmentInfo = localStore.getAttachmentInfo(id); + if (MimeUtility.DEFAULT_ATTACHMENT_MIME_TYPE.equalsIgnoreCase(attachmentInfo.type)) { + // If the MIME type is the generic "application/octet-stream" + // we try to find a better one by looking at the file extension. + return MimeUtility.getMimeTypeByExtension(attachmentInfo.name); + } else { + // Some messages contain wrong MIME types. See if we know better. + return MimeUtility.canonicalizeMimeType(attachmentInfo.type); + } + } catch (MessagingException e) { Log.e(K9.LOG_TAG, "Unable to retrieve LocalStore for " + account, e); return null;