2

I have a String which contains path to the some file, for ex. "/sdcard0/Lewis/cache.obb".

How I can get file extension (w/out dot) from that String?

Note: file extension can be different, for ex. ".tar.gz".

Roman C
  • 48,723
  • 33
  • 63
  • 158
Helisia
  • 568
  • 2
  • 6
  • 22

2 Answers2

6

If you have the file path/URL, you can use MimeTypeMap#getMimeTypeFromExtension() like this:

public static String getFileType(String url)
{
    String type = null;
    String extension = MimeTypeMap.getFileExtensionFromUrl(url);
    if (extension != null) {
        MimeTypeMap mime = MimeTypeMap.getSingleton();
        type = mime.getMimeTypeFromExtension(extension);
    }
    return type;
}

And then you just call it like this:

String fileType = YourClass.getFileType(file);
hichris123
  • 9,955
  • 15
  • 53
  • 68
0

Try this if (fileName != null) { File imageFile = new File(fileName);

String fileType=fileName.substring(imageFile.getName().length(),fileName.length());

}