Fix bug on getting MIME type for external Persistent Blob URI's

Fixes #5295
Closes #5300
// FREEBIE
This commit is contained in:
Geonu Kang 2016-02-29 15:23:35 +09:00 committed by Moxie Marlinspike
parent ba960a82e2
commit a7cbe5e2a2

View file

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