From e83827ab75d070d5743308528a774e7df80c3fc9 Mon Sep 17 00:00:00 2001 From: Geonu Kang Date: Sat, 21 Nov 2015 17:04:19 +0900 Subject: [PATCH] "image/jpg"->"image/jpeg" on MIME type checks and saving Fixes #4602 Closes #4643 --- .../thoughtcrime/securesms/ShareActivity.java | 16 ++++++---------- .../thoughtcrime/securesms/util/MediaUtil.java | 15 ++++++++++++++- .../securesms/util/SaveAttachmentTask.java | 8 ++++---- 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/src/org/thoughtcrime/securesms/ShareActivity.java b/src/org/thoughtcrime/securesms/ShareActivity.java index 68e3af90d3a..367c15c4890 100644 --- a/src/org/thoughtcrime/securesms/ShareActivity.java +++ b/src/org/thoughtcrime/securesms/ShareActivity.java @@ -29,7 +29,6 @@ import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; -import android.webkit.MimeTypeMap; import org.thoughtcrime.securesms.crypto.MasterSecret; import org.thoughtcrime.securesms.mms.PartAuthority; @@ -37,6 +36,7 @@ import org.thoughtcrime.securesms.recipients.Recipients; import org.thoughtcrime.securesms.util.DynamicLanguage; import org.thoughtcrime.securesms.util.DynamicTheme; +import org.thoughtcrime.securesms.util.MediaUtil; import org.thoughtcrime.securesms.util.ViewUtil; import java.io.IOException; @@ -167,7 +167,8 @@ private Intent getBaseShareIntent(final @NonNull Class target) { final Intent intent = new Intent(this, target); final String textExtra = getIntent().getStringExtra(Intent.EXTRA_TEXT); final Uri streamExtra = getIntent().getParcelableExtra(Intent.EXTRA_STREAM); - final String type = streamExtra != null ? getMimeType(streamExtra) : getIntent().getType(); + final String type = streamExtra != null ? getMimeType(streamExtra) + : MediaUtil.getCorrectedMimeType(getIntent().getType()); intent.putExtra(ConversationActivity.TEXT_EXTRA, textExtra); if (resolvedExtra != null) intent.setDataAndType(resolvedExtra, type); @@ -175,14 +176,9 @@ private Intent getBaseShareIntent(final @NonNull Class target) { } private String getMimeType(Uri uri) { - String type = getContentResolver().getType(uri); - - if (type == null) { - String extension = MimeTypeMap.getFileExtensionFromUrl(uri.toString()); - type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension); - } - - return type == null ? getIntent().getType() : type; + final String type = MediaUtil.getMimeType(getApplicationContext(), uri); + return type == null ? MediaUtil.getCorrectedMimeType(getIntent().getType()) + : type; } private class ResolveMediaTask extends AsyncTask { diff --git a/src/org/thoughtcrime/securesms/util/MediaUtil.java b/src/org/thoughtcrime/securesms/util/MediaUtil.java index ddd72247af6..6caaa831730 100644 --- a/src/org/thoughtcrime/securesms/util/MediaUtil.java +++ b/src/org/thoughtcrime/securesms/util/MediaUtil.java @@ -76,7 +76,20 @@ public static Slide getSlideForAttachment(Context context, Attachment attachment final String extension = MimeTypeMap.getFileExtensionFromUrl(uri.toString()); type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension); } - return type; + return getCorrectedMimeType(type); + } + + public static @Nullable String getCorrectedMimeType(@Nullable String mimeType) { + if (mimeType == null) return null; + + switch(mimeType) { + case "image/jpg": + return MimeTypeMap.getSingleton().hasMimeType(ContentType.IMAGE_JPEG) + ? ContentType.IMAGE_JPEG + : mimeType; + default: + return mimeType; + } } public static long getMediaSize(Context context, MasterSecret masterSecret, Uri uri) throws IOException { diff --git a/src/org/thoughtcrime/securesms/util/SaveAttachmentTask.java b/src/org/thoughtcrime/securesms/util/SaveAttachmentTask.java index 6072414b1f8..8c4f287cec3 100644 --- a/src/org/thoughtcrime/securesms/util/SaveAttachmentTask.java +++ b/src/org/thoughtcrime/securesms/util/SaveAttachmentTask.java @@ -58,7 +58,8 @@ protected Integer doInBackground(SaveAttachmentTask.Attachment... attachments) { return FAILURE; } - File mediaFile = constructOutputFile(attachment.contentType, attachment.date); + String contentType = MediaUtil.getCorrectedMimeType(attachment.contentType); + File mediaFile = constructOutputFile(contentType, attachment.date); InputStream inputStream = PartAuthority.getAttachmentStream(context, masterSecret, attachment.uri); if (inputStream == null) { @@ -69,7 +70,7 @@ protected Integer doInBackground(SaveAttachmentTask.Attachment... attachments) { Util.copy(inputStream, outputStream); MediaScannerConnection.scanFile(context, new String[]{mediaFile.getAbsolutePath()}, - new String[]{attachment.contentType}, null); + new String[]{contentType}, null); return SUCCESS; } catch (IOException ioe) { @@ -121,8 +122,7 @@ private File constructOutputFile(String contentType, long timestamp) throws IOEx SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd-HHmmss"); String base = "signal-" + dateFormatter.format(timestamp); - if (extension == null) - extension = "attach"; + if (extension == null) extension = "attach"; int i = 0; File file = new File(outputDirectory, base + "." + extension);