diff --git a/src/com/android/email/activity/MessageView.java b/src/com/android/email/activity/MessageView.java index 6de1f45e1..ae2ca59d1 100644 --- a/src/com/android/email/activity/MessageView.java +++ b/src/com/android/email/activity/MessageView.java @@ -714,6 +714,29 @@ public class MessageView extends Activity } } + /** + * Resolve attachment id to content URI. + * + * @param attachmentUri + * @return resolved content URI + */ + private Uri resolveAttachmentIdToContentUri(long attachmentId) { + Uri attachmentUri = AttachmentProvider.getAttachmentUri(mAccount, attachmentId); + Cursor c = getContentResolver().query(attachmentUri, + new String[] { AttachmentProvider.AttachmentProviderColumns.DATA }, + null, null, null); + if (c != null) { + try { + if (c.moveToFirst()) { + return Uri.parse(c.getString(0)); + } + } finally { + c.close(); + } + } + return attachmentUri; + } + /** * Resolve content-id reference in src attribute of img tag to AttachmentProvider's * content uri. This method calls itself recursively at most the number of @@ -737,15 +760,11 @@ public class MessageView extends Activity contentId != null && part instanceof LocalAttachmentBodyPart) { LocalAttachmentBodyPart attachment = (LocalAttachmentBodyPart)part; - Uri contentUri = AttachmentProvider.getAttachmentUri( - mAccount, - attachment.getAttachmentId()); - if (contentUri != null) { - // Regexp which matches ' src="cid:contentId"'. - String contentIdRe = "\\s+(?i)src=\"cid(?-i):\\Q" + contentId + "\\E\""; - // Replace all occurrences of src attribute with ' src="content://contentUri"'. - text = text.replaceAll(contentIdRe, " src=\"" + contentUri + "\""); - } + Uri contentUri = resolveAttachmentIdToContentUri(attachment.getAttachmentId()); + // Regexp which matches ' src="cid:contentId"'. + String contentIdRe = "\\s+(?i)src=\"cid(?-i):\\Q" + contentId + "\\E\""; + // Replace all occurrences of src attribute with ' src="content://contentUri"'. + text = text.replaceAll(contentIdRe, " src=\"" + contentUri + "\""); } if (part.getBody() instanceof Multipart) { @@ -1047,9 +1066,7 @@ public class MessageView extends Activity try { File file = createUniqueFile(Environment.getExternalStorageDirectory(), attachment.name); - Uri uri = AttachmentProvider.getAttachmentUri( - mAccount, - attachment.part.getAttachmentId()); + Uri uri = resolveAttachmentIdToContentUri(attachment.part.getAttachmentId()); InputStream in = getContentResolver().openInputStream(uri); OutputStream out = new FileOutputStream(file); IOUtils.copy(in, out); @@ -1065,9 +1082,7 @@ public class MessageView extends Activity } else { try { - Uri uri = AttachmentProvider.getAttachmentUri( - mAccount, - attachment.part.getAttachmentId()); + Uri uri = resolveAttachmentIdToContentUri(attachment.part.getAttachmentId()); Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(uri); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); diff --git a/src/com/android/email/mail/store/LocalStore.java b/src/com/android/email/mail/store/LocalStore.java index 71298cb2e..026e0cf4e 100644 --- a/src/com/android/email/mail/store/LocalStore.java +++ b/src/com/android/email/mail/store/LocalStore.java @@ -1339,6 +1339,7 @@ public class LocalStore extends Store implements PersistentDataCallbacks { cv.put("content_uri", contentUri != null ? contentUri.toString() : null); cv.put("size", size); cv.put("content_id", contentId); + cv.put("message_id", messageId); mDb.update( "attachments", cv, diff --git a/src/com/android/email/provider/AttachmentProvider.java b/src/com/android/email/provider/AttachmentProvider.java index 7fa9be914..6cc187c50 100644 --- a/src/com/android/email/provider/AttachmentProvider.java +++ b/src/com/android/email/provider/AttachmentProvider.java @@ -155,12 +155,23 @@ public class AttachmentProvider extends ContentProvider { File file = new File(dir, filename); if (!file.exists()) { Uri attachmentUri = getAttachmentUri(dbName, Long.parseLong(id)); - String type = getType(attachmentUri); + Cursor c = query(attachmentUri, + new String[] { AttachmentProviderColumns.DATA }, null, null, null); + if (c != null) { + try { + if (c.moveToFirst()) { + attachmentUri = Uri.parse(c.getString(0)); + } + } finally { + c.close(); + } + } + String type = getContext().getContentResolver().getType(attachmentUri); try { - FileInputStream in = new FileInputStream( - new File(getContext().getDatabasePath(dbName + "_att"), id)); + InputStream in = + getContext().getContentResolver().openInputStream(attachmentUri); Bitmap thumbnail = createThumbnail(type, in); - thumbnail = thumbnail.createScaledBitmap(thumbnail, width, height, true); + thumbnail = Bitmap.createScaledBitmap(thumbnail, width, height, true); FileOutputStream out = new FileOutputStream(file); thumbnail.compress(Bitmap.CompressFormat.PNG, 100, out); out.close(); @@ -207,13 +218,14 @@ public class AttachmentProvider extends ContentProvider { String path = getContext().getDatabasePath(dbName).getAbsolutePath(); String name = null; int size = -1; + String contentUri = null; SQLiteDatabase db = null; Cursor cursor = null; try { db = SQLiteDatabase.openDatabase(path, null, 0); cursor = db.query( "attachments", - new String[] { "name", "size" }, + new String[] { "name", "size", "content_uri" }, "id = ?", new String[] { id }, null, @@ -224,6 +236,7 @@ public class AttachmentProvider extends ContentProvider { } name = cursor.getString(0); size = cursor.getInt(1); + contentUri = cursor.getString(2); } finally { if (cursor != null) { @@ -242,7 +255,7 @@ public class AttachmentProvider extends ContentProvider { values[i] = id; } else if (AttachmentProviderColumns.DATA.equals(column)) { - values[i] = uri.toString(); + values[i] = contentUri; } else if (AttachmentProviderColumns.DISPLAY_NAME.equals(column)) { values[i] = name; diff --git a/tests/src/com/android/email/activity/MessageViewTests.java b/tests/src/com/android/email/activity/MessageViewTests.java index fd576ddbe..f7a14af08 100644 --- a/tests/src/com/android/email/activity/MessageViewTests.java +++ b/tests/src/com/android/email/activity/MessageViewTests.java @@ -30,8 +30,10 @@ import com.android.email.mail.MessageTestUtils.TextBuilder; import com.android.email.mail.internet.BinaryTempFileBody; import com.android.email.mail.store.LocalStore; +import android.content.ContentResolver; import android.content.Context; import android.content.Intent; +import android.database.sqlite.SQLiteDatabase; import android.net.Uri; import android.test.ActivityInstrumentationTestCase2; import android.test.suitebuilder.annotation.MediumTest; @@ -236,8 +238,20 @@ public class MessageViewTests final String actual5 = a.resolveInlineImage(null, msg4, 0); assertNull(actual5); } - - + + /** + * Test for resolveAttachmentIdToContentUri. + */ + public void testResolveAttachmentIdToContentUri() throws MessagingException, IOException { + final ContentResolver contentResolver = mContext.getContentResolver(); + final MessageView a = getActivity(); + // create attachments tables. + LocalStore.newInstance(mAccount.getLocalStoreUri(), mContext, null); + final String dbPath = mContext.getDatabasePath(mAccount.getUuid() + ".db").toString(); + final SQLiteDatabase db = SQLiteDatabase.openDatabase(dbPath, null, 0); + // TODO write unit test + } + /** * Mock Messaging controller, so we can drive its callbacks. This probably should be * generalized since we're likely to use for other tests eventually.