From a7cbe5e2a2c447b78a699ec4a9660e9ed6a83fbd Mon Sep 17 00:00:00 2001 From: Geonu Kang Date: Mon, 29 Feb 2016 15:23:35 +0900 Subject: [PATCH] Fix bug on getting MIME type for external Persistent Blob URI's Fixes #5295 Closes #5300 // FREEBIE --- .../providers/PersistentBlobProvider.java | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/org/thoughtcrime/securesms/providers/PersistentBlobProvider.java b/src/org/thoughtcrime/securesms/providers/PersistentBlobProvider.java index 2ee0d206ed..30dc5acafe 100644 --- a/src/org/thoughtcrime/securesms/providers/PersistentBlobProvider.java +++ b/src/org/thoughtcrime/securesms/providers/PersistentBlobProvider.java @@ -134,7 +134,9 @@ public class PersistentBlobProvider { public static @Nullable String getMimeType(@NonNull Context context, @NonNull Uri persistentBlobUri) { if (!isAuthority(context, persistentBlobUri)) return null; - return persistentBlobUri.getPathSegments().get(MIMETYPE_PATH_SEGMENT); + return isExternalBlobUri(context, persistentBlobUri) + ? getMimeTypeFromExtension(persistentBlobUri) + : persistentBlobUri.getPathSegments().get(MIMETYPE_PATH_SEGMENT); } private static @NonNull String getExtensionFromMimeType(String mimeType) { @@ -142,6 +144,12 @@ public class PersistentBlobProvider { return extension != null ? extension : BLOB_EXTENSION; } + private static @NonNull String getMimeTypeFromExtension(@NonNull Uri uri) { + final String mimeType = MimeTypeMap.getSingleton() + .getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(uri.toString())); + return mimeType != null ? mimeType : "application/octet-stream"; + } + private static @NonNull File getExternalDir(Context context) throws IOException { final File externalDir = context.getExternalFilesDir(null); if (externalDir == null) throw new IOException("no external files directory"); @@ -149,11 +157,14 @@ public class PersistentBlobProvider { } public static boolean isAuthority(@NonNull Context context, @NonNull Uri uri) { + return MATCHER.match(uri) == MATCH || isExternalBlobUri(context, uri); + } + + private static boolean isExternalBlobUri(@NonNull Context context, @NonNull Uri uri) { try { - return MATCHER.match(uri) == MATCH || uri.getPath().startsWith(getExternalDir(context).getAbsolutePath()); + return uri.getPath().startsWith(getExternalDir(context).getAbsolutePath()); } catch (IOException ioe) { return false; } } - }