I am trying to retrieve an extension from a file using MimeTypeMap.class of android.webkit package.
File file = new File(someUrl);
String ext = MimeTypeMap.getSingleton()
.getFileExtensionFromUrl(file.getAbsolutePath());
ext returns null or empty if the file.getName() contains blank spaces!
How to solve it?
you can try
public static String getMimeType(File file) {
String extension = getExtension(file.getName());
if (extension.length() > 0)
return MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension.substring(1));
return "application/octet-stream";
}
public static String getExtension(String uri) {
if (uri == null) {
return null;
}
int dot = uri.lastIndexOf(".");
if (dot >= 0) {
return uri.substring(dot);
} else {
// No extension.
return "";
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With