Categories
android

How to check file extension on Android

In my application, when we browse the file from the SD card, the files will be .text, .jpg and mpeg4ie video files.

I want to store each file type into a particular folder. For example, .text files go to the text folder. When I select the file, how do I check the file extension?

I would get the file name as a String, split it into an array with “.” as the delimiter, and then get the last index of the array, which would be the file extension. For example:

public class main {
public static void main(String[] args) {
String filename = "image.jpg";
String filenameArray[] = filename.split("\\.");
String extension = filenameArray[filenameArray.length-1];
System.out.println(extension);
}
}

Which outputs:

jpg