diff --git a/src/com/fsck/k9/mail/store/LocalStore.java b/src/com/fsck/k9/mail/store/LocalStore.java index abfa3ed1d..38130c509 100644 --- a/src/com/fsck/k9/mail/store/LocalStore.java +++ b/src/com/fsck/k9/mail/store/LocalStore.java @@ -2692,22 +2692,34 @@ public class LocalStore extends Store implements Serializable { public Void doDbWork(final SQLiteDatabase db) throws WrappedException, UnavailableStorageException { Cursor attachmentsCursor = null; try { - attachmentsCursor = db.query("attachments", new String[] - { "id" }, "message_id = ?", new String[] - { Long.toString(messageId) }, null, null, null); + String accountUuid = mAccount.getUuid(); + Context context = mApplication; + + // Get attachment IDs + String[] whereArgs = new String[] { Long.toString(messageId) }; + attachmentsCursor = db.query("attachments", new String[] { "id" }, + "message_id = ?", whereArgs, null, null, null); + final File attachmentDirectory = StorageManager.getInstance(mApplication) - .getAttachmentDirectory(uUid, database.getStorageProviderId()); + .getAttachmentDirectory(uUid, database.getStorageProviderId()); + while (attachmentsCursor.moveToNext()) { - long attachmentId = attachmentsCursor.getLong(0); + String attachmentId = Long.toString(attachmentsCursor.getLong(0)); try { - File file = new File(attachmentDirectory, Long.toString(attachmentId)); + // Delete stored attachment + File file = new File(attachmentDirectory, attachmentId); if (file.exists()) { file.delete(); } - } catch (Exception e) { - } + // Delete thumbnail file + AttachmentProvider.deleteThumbnail(context, accountUuid, + attachmentId); + } catch (Exception e) { /* ignore */ } } + + // Delete attachment metadata from the database + db.delete("attachments", "message_id = ?", whereArgs); } finally { Utility.closeQuietly(attachmentsCursor); } diff --git a/src/com/fsck/k9/provider/AttachmentProvider.java b/src/com/fsck/k9/provider/AttachmentProvider.java index 4b90305c7..dd380e6af 100644 --- a/src/com/fsck/k9/provider/AttachmentProvider.java +++ b/src/com/fsck/k9/provider/AttachmentProvider.java @@ -92,6 +92,30 @@ public class AttachmentProvider extends ContentProvider { } } + /** + * Delete the thumbnail of an attachment. + * + * @param context + * The application context. + * @param accountUuid + * The UUID of the account the attachment belongs to. + * @param attachmentId + * The ID of the attachment the thumbnail was created for. + */ + public static void deleteThumbnail(Context context, String accountUuid, String attachmentId) { + File file = getThumbnailFile(context, accountUuid, attachmentId); + if (file.exists()) { + file.delete(); + } + } + + private static File getThumbnailFile(Context context, String accountUuid, + String attachmentId) { + String filename = "thmb_" + accountUuid + "_" + attachmentId + ".tmp"; + File dir = context.getCacheDir(); + return new File(dir, filename); + } + @Override public boolean onCreate() { @@ -139,9 +163,7 @@ public class AttachmentProvider extends ContentProvider { int width = Integer.parseInt(segments.get(3)); int height = Integer.parseInt(segments.get(4)); - String filename = "thmb_" + accountUuid + "_" + attachmentId + ".tmp"; - File dir = getContext().getCacheDir(); - file = new File(dir, filename); + file = getThumbnailFile(getContext(), accountUuid, attachmentId); if (!file.exists()) { String type = getType(accountUuid, attachmentId, FORMAT_VIEW); try {