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
  • 49,761
  • 33
  • 66
  • 176
Helisia
  • 568
  • 2
  • 7
  • 23

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
  • 10,145
  • 15
  • 56
  • 70
0

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

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

}

M. Usman Afzal
  • 103
  • 1
  • 2
  • 8