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>
This commit is contained in:
Taesu Lee
2020-02-03 11:04:15 +09:00
parent 3bd7b79daa
commit 0770975c8a
3 changed files with 23 additions and 13 deletions

View File

@@ -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);

View File

@@ -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

View File

@@ -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);