This works for me:
public static String getExtension(String fileName) {
String encoded;
try { encoded = URLEncoder.encode(fileName, "UTF-8").replace("+", "%20"); }
catch(UnsupportedEncodingException e) { encoded = fileName; }
return MimeTypeMap.getFileExtensionFromUrl(encoded).toLowerCase();
}
When the file is /mnt/sdcard/boomerang/2013-06-18_12:08:53.txt
, "txt"
is returned.
Note the URLEncoder.encode
and .repalce
calls should fix any reliability issues you might see when MimeTypeMap.getFileExtensionFromUrl
is called by itself. For example, without the encoding and replace calls, file names such as "Test Image!.jpg"
return empty strings. Also make sure the file name is lowercased. It MimeTypeMap
does not seem to have entries for .PNG
or .JPG
.