diff options
author | Taesu Lee <taesu82.lee@samsung.com> | 2020-02-03 11:04:15 +0900 |
---|---|---|
committer | Luca Stefani <luca.stefani.ge1@gmail.com> | 2020-02-07 16:26:02 +0100 |
commit | 23c53d98e861cb2aee78f0d38ee88d77354982fb (patch) | |
tree | 162a4cae7991e3032d7200c48ea7102158d230e8 | |
parent | a96facde54eaaa445d9b0dbe5fe1d25d40ed0005 (diff) | |
download | packages_apps_Messaging-23c53d98e861cb2aee78f0d38ee88d77354982fb.tar.gz packages_apps_Messaging-23c53d98e861cb2aee78f0d38ee88d77354982fb.tar.bz2 packages_apps_Messaging-23c53d98e861cb2aee78f0d38ee88d77354982fb.zip |
Use file extension based on MIME type
It uses the most common extension for the given MIME type.
Test: Manual
Change-Id: I3d4aea85fcd42088ed19898054db7225954d3401
Signed-off-by: Taesu Lee <taesu82.lee@samsung.com>
-rw-r--r-- | src/com/android/messaging/sms/MmsUtils.java | 25 | ||||
-rw-r--r-- | src/com/android/messaging/util/ContentType.java | 6 | ||||
-rw-r--r-- | src/com/android/messaging/util/FileUtil.java | 5 |
3 files changed, 23 insertions, 13 deletions
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 @@ public class MmsUtils { 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 @@ public class MmsUtils { 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/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 @@ public final class ContentType { 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.content.Context; 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 @@ public class FileUtil { * 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); |