Merge "Fix NPE caused by invalid IFD0 offset"
diff --git a/src/com/android/messaging/sms/MmsUtils.java b/src/com/android/messaging/sms/MmsUtils.java
index fbecd8b..df0db34 100644
--- a/src/com/android/messaging/sms/MmsUtils.java
+++ b/src/com/android/messaging/sms/MmsUtils.java
@@ -333,20 +333,26 @@
             String srcName;
             if (part.isAttachment()) {
                 String contentType = part.getContentType();
+                final String extension = ContentType.getExtensionFromMimeType(contentType);
                 if (ContentType.isImageType(contentType)) {
-                    // There's a good chance that if we selected the image from our media picker the
-                    // content type is image/*. Fix the content type here for gifs so that we only
-                    // need to open the input stream once. All other gif vs static image checks will
-                    // only have to do a string comparison which is much cheaper.
-                    final boolean isGif = ImageUtils.isGif(contentType, part.getContentUri());
-                    contentType = isGif ? ContentType.IMAGE_GIF : contentType;
-                    srcName = String.format(isGif ? "image%06d.gif" : "image%06d.jpg", index);
+                    if (extension != null) {
+                        srcName = String.format("image%06d.%s", index, extension);
+                    } else {
+                        // There's a good chance that if we selected the image from our media picker
+                        // the content type is image/*. Fix the content type here for gifs so that
+                        // we only need to open the input stream once. All other gif vs static image
+                        // checks will only have to do a string comparison which is much cheaper.
+                        final boolean isGif = ImageUtils.isGif(contentType, part.getContentUri());
+                        contentType = isGif ? ContentType.IMAGE_GIF : contentType;
+                        srcName = String.format(isGif ? "image%06d.gif" : "image%06d.jpg", index);
+                    }
                     smilBody.append(String.format(sSmilImagePart, srcName));
                     totalLength += addPicturePart(context, pb, index, part,
                             widthLimit, heightLimit, bytesPerImage, srcName, contentType);
                     hasVisualAttachment = true;
                 } else if (ContentType.isVideoType(contentType)) {
-                    srcName = String.format("video%06d.mp4", index);
+                    srcName = String.format("video%06d.%s", index,
+                            extension != null ? extension : "mp4");
                     final int length = addVideoPart(context, pb, part, srcName);
                     totalLength += length;
                     smilBody.append(String.format(sSmilVideoPart, srcName,
@@ -358,7 +364,8 @@
                     smilBody.append(String.format(sSmilPart, srcName));
                     hasNonVisualAttachment = true;
                 } else if (ContentType.isAudioType(contentType)) {
-                    srcName = String.format("recording%06d.amr", index);
+                    srcName = String.format("recording%06d.%s",
+                            index, extension != null ? extension : "amr");
                     totalLength += addOtherPart(context, pb, part, srcName);
                     final int duration = getMediaDurationMs(context, part, -1);
                     Assert.isTrue(duration != -1);
diff --git a/src/com/android/messaging/ui/AsyncImageView.java b/src/com/android/messaging/ui/AsyncImageView.java
index e37966a..7d057b3 100644
--- a/src/com/android/messaging/ui/AsyncImageView.java
+++ b/src/com/android/messaging/ui/AsyncImageView.java
@@ -125,6 +125,8 @@
                 return;
             }
             unbindView();
+        } else {
+            mDetachedRequestDescriptor = null;
         }
         setImage(null);
         resetTransientViewStates();
diff --git a/src/com/android/messaging/util/ContentType.java b/src/com/android/messaging/util/ContentType.java
index bb4a7b2..75fff93 100644
--- a/src/com/android/messaging/util/ContentType.java
+++ b/src/com/android/messaging/util/ContentType.java
@@ -168,6 +168,12 @@
         return contentType;
     }
 
+    public static String getExtensionFromMimeType(final String mimeType) {
+        final MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
+        final String extension = mimeTypeMap.getExtensionFromMimeType(mimeType);
+        return extension;
+    }
+
     /**
      * Get the common file extension for a given content type
      * @param contentType The content type
diff --git a/src/com/android/messaging/util/FileUtil.java b/src/com/android/messaging/util/FileUtil.java
index e35e79b..7d59aa7 100644
--- a/src/com/android/messaging/util/FileUtil.java
+++ b/src/com/android/messaging/util/FileUtil.java
@@ -21,7 +21,6 @@
 import android.net.Uri;
 import android.os.Environment;
 import android.text.TextUtils;
-import android.webkit.MimeTypeMap;
 
 import com.android.messaging.Factory;
 import com.android.messaging.R;
@@ -62,9 +61,7 @@
      *   actually creating the file.
      */
     public static File getNewFile(File directory, String contentType) throws IOException {
-        MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
-        String fileExtension = mimeTypeMap.getExtensionFromMimeType(contentType);
-
+        String fileExtension = ContentType.getExtensionFromMimeType(contentType);
         final Context context = Factory.get().getApplicationContext();
         String fileNameFormat = context.getString(ContentType.isImageType(contentType)
                 ? R.string.new_image_file_name_format : R.string.new_file_name_format);