Android: guessContentTypeFromStream()

September 10, 2016

If you ever used Android’s implementation of URLConnection.guessContentTypeFromStream(), guess what? Android’s implementation of it is a VERY limited version of the Java version. Take a look at android’s code at like 695 onward here.

It only returns 4 different strings from its guess. They are, application/zip,image/gif, text/html and application/xml. If you have a PNG image, it won’t guess it!

So how do you improve the guessing in Android? Well, you got to improve the guessContentTypeFromStream() in Android by using the Java’s implementation. You can check it out here.

From there, you can improve it with the additional guesses that was implemented in Java.

Go forth and create this method if you need it in Android.

Learned this Android Quirk from https://github.com/appcelerator/titanium_mobile/pull/6216


Note: This article was original written by me in my Wordpress blog. I’m currently trying to move all articles here.

comments powered by Disqus