Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MimeTypeMap fail to return extension for a filename that contains spaces

Tags:

java

android

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?

like image 603
Francis Nduba Numbi Avatar asked Nov 18 '22 11:11

Francis Nduba Numbi


1 Answers

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 "";
        }
    }
like image 117
Nasimxon Avatar answered Jan 20 '23 15:01

Nasimxon