MinetypeMap.getFileExtensionFromUrl () Kotlin

You can use this method to get File extension. Then you can get MimeType from extension

In Java

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();
}
In Kotlin

fun getExtension(fileName: String): String {
    val encoded: String = try {
        URLEncoder.encode(fileName, "UTF-8").replace("+", "%20")
    } catch (e: Exception) {
        fileName
    }

    return MimeTypeMap.getFileExtensionFromUrl(encoded).toLowerCase()
}
or as Kotlin extension:

fun File.getExtension(): String {
    val encoded: String = try {
        URLEncoder.encode(name, "UTF-8").replace("+", "%20")
    } catch (e: Exception) {
        name
    }

    return MimeTypeMap.getFileExtensionFromUrl(encoded).toLowerCase(Locale.getDefault())
}
android developer