Merge "Use file extension based on MIME type" am: 5c8f5fdeb1 am: 5002cebb6e
Change-Id: I43642aabb5e52426e07d3081eda78b17894bbc58
This commit is contained in:
@@ -333,20 +333,26 @@ public class MmsUtils {
|
|||||||
String srcName;
|
String srcName;
|
||||||
if (part.isAttachment()) {
|
if (part.isAttachment()) {
|
||||||
String contentType = part.getContentType();
|
String contentType = part.getContentType();
|
||||||
|
final String extension = ContentType.getExtensionFromMimeType(contentType);
|
||||||
if (ContentType.isImageType(contentType)) {
|
if (ContentType.isImageType(contentType)) {
|
||||||
// There's a good chance that if we selected the image from our media picker the
|
if (extension != null) {
|
||||||
// content type is image/*. Fix the content type here for gifs so that we only
|
srcName = String.format("image%06d.%s", index, extension);
|
||||||
// need to open the input stream once. All other gif vs static image checks will
|
} else {
|
||||||
// only have to do a string comparison which is much cheaper.
|
// There's a good chance that if we selected the image from our media picker
|
||||||
final boolean isGif = ImageUtils.isGif(contentType, part.getContentUri());
|
// the content type is image/*. Fix the content type here for gifs so that
|
||||||
contentType = isGif ? ContentType.IMAGE_GIF : contentType;
|
// we only need to open the input stream once. All other gif vs static image
|
||||||
srcName = String.format(isGif ? "image%06d.gif" : "image%06d.jpg", index);
|
// 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));
|
smilBody.append(String.format(sSmilImagePart, srcName));
|
||||||
totalLength += addPicturePart(context, pb, index, part,
|
totalLength += addPicturePart(context, pb, index, part,
|
||||||
widthLimit, heightLimit, bytesPerImage, srcName, contentType);
|
widthLimit, heightLimit, bytesPerImage, srcName, contentType);
|
||||||
hasVisualAttachment = true;
|
hasVisualAttachment = true;
|
||||||
} else if (ContentType.isVideoType(contentType)) {
|
} 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);
|
final int length = addVideoPart(context, pb, part, srcName);
|
||||||
totalLength += length;
|
totalLength += length;
|
||||||
smilBody.append(String.format(sSmilVideoPart, srcName,
|
smilBody.append(String.format(sSmilVideoPart, srcName,
|
||||||
@@ -358,7 +364,8 @@ public class MmsUtils {
|
|||||||
smilBody.append(String.format(sSmilPart, srcName));
|
smilBody.append(String.format(sSmilPart, srcName));
|
||||||
hasNonVisualAttachment = true;
|
hasNonVisualAttachment = true;
|
||||||
} else if (ContentType.isAudioType(contentType)) {
|
} 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);
|
totalLength += addOtherPart(context, pb, part, srcName);
|
||||||
final int duration = getMediaDurationMs(context, part, -1);
|
final int duration = getMediaDurationMs(context, part, -1);
|
||||||
Assert.isTrue(duration != -1);
|
Assert.isTrue(duration != -1);
|
||||||
|
|||||||
@@ -168,6 +168,12 @@ public final class ContentType {
|
|||||||
return 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
|
* Get the common file extension for a given content type
|
||||||
* @param contentType The content type
|
* @param contentType The content type
|
||||||
|
|||||||
@@ -21,7 +21,6 @@ import android.content.Context;
|
|||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Environment;
|
import android.os.Environment;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
import android.webkit.MimeTypeMap;
|
|
||||||
|
|
||||||
import com.android.messaging.Factory;
|
import com.android.messaging.Factory;
|
||||||
import com.android.messaging.R;
|
import com.android.messaging.R;
|
||||||
@@ -62,9 +61,7 @@ public class FileUtil {
|
|||||||
* actually creating the file.
|
* actually creating the file.
|
||||||
*/
|
*/
|
||||||
public static File getNewFile(File directory, String contentType) throws IOException {
|
public static File getNewFile(File directory, String contentType) throws IOException {
|
||||||
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
|
String fileExtension = ContentType.getExtensionFromMimeType(contentType);
|
||||||
String fileExtension = mimeTypeMap.getExtensionFromMimeType(contentType);
|
|
||||||
|
|
||||||
final Context context = Factory.get().getApplicationContext();
|
final Context context = Factory.get().getApplicationContext();
|
||||||
String fileNameFormat = context.getString(ContentType.isImageType(contentType)
|
String fileNameFormat = context.getString(ContentType.isImageType(contentType)
|
||||||
? R.string.new_image_file_name_format : R.string.new_file_name_format);
|
? R.string.new_image_file_name_format : R.string.new_file_name_format);
|
||||||
|
|||||||
Reference in New Issue
Block a user