Android: Get file mime type from filename

Man, I wish it were this easy in all languages and platforms!

import android.webkit.MimeTypeMap;

MimeTypeMap.getSingleton().getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(source.substring(source.lastIndexOf("."))))

Where source would be either a filename or URL.

*same day edit*

Wow, that's a first I've had to update a post a few hours after putting it up.

There was a bug with filenames containing spaces, causing getFileExtensionsFromUrl() to return null.

Sources:

 
Copyright © Twig's Tech Tips
Theme by BloggerThemes & TopWPThemes Sponsored by iBlogtoBlog