How to check file extension on Android

how to separate filename and extension in java
android get file from content uri
validate file extension in java
get file name from uri android
java get file type without extension
get file size from uri android
android file extension list
android get pdf file path from content uri

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];

Which outputs:


Retrieving file information, Retrieve a file's MIME type; Retrieve a file's name and size The data type helps the client app to determine if it can handle the file, and the file  I have found a nice example at on how to get the file extension that returns the file type extension in lower case or null if there isn't a file extension: /* * Get the extension of a file.

public static String getFileExt(String fileName) {
    return fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length());

How to Change File Extension on Samsung Android Phones , Now you know how to change file extension on Samsung Android phones for free using the above free file explorer app. Steps to rename files, change file  From the Browse Button, when a file is clicked it stores the folder path in which that file is in one string and the file name without extension in other string, which I am using to store, either of these two, in the EditText.

Or you could just do:


Note: this method is not very reliable though...

A Guide on File Extension in Android and Windows 10 (2020 , Tap the Set Default View Settings option. Check (to show) or uncheck (to hide) Show file extensions which you can find between Show  If you’re using a device with stock Android 6.x (Marshmallow) or newer, there’s a built-in file manager…it’s just hidden away in the Settings. Head to Settings > Storage > Other and you’ll have a full list of all the files and folders on your internal storage.

You can use FilenameUtils from Apache Commons:

String extension = FilenameUtils.getExtension(mFile.getName());

How to Get File Extension in Java, Java Get File Extension, How to get a file extension in java program, Java File However, in real programming scenarios, you should check that file exists or not JournalDev is one of the most popular websites for Java, Python, Android, and​  Step 1: Open File Explorer, and then click File on the menu bar. On the drop-down menu, click Change Folder and Search Options. Step 2: On the Folder Options dialog box, switch to the View tab. Within the Advanced Settings section, uncheck the box next to Hide Extensions for Known File Types.

You could also do something like the below and use endsWith.

public void walkdir(File dir) {
    String txtPattern = ".txt";
    String jpgPattern = ".jpg";
    String mp3Pattern = ".mp3";

    File listFile[] = dir.listFiles();

    if (listFile != null) {
        for (int i = 0; i < listFile.length; i++) {

            if (listFile[i].isDirectory()) {
            else {
                if (listFile[i].getName().endsWith(txtPattern)){
                    // Put in txt folder

                } else if (listFile[i].getName().endsWith(jpgPattern.toLowerCase())){
                    // Put in jpg folder

                } else if (istFile[i].getName().endsWith(mp3Pattern.toLowerCase())) {
                    // Put in mp3 folder

Sorry, I haven't checked the work, but it should do it for you.

How To Change The File Extension On Android, Get File Extension And MIME Type In Android Development @Override protected void onListItemClick(ListView l, View v, int position, long id)  Recommended software programs are sorted by OS platform (Windows, macOS, Linux, iOS, Android etc.) and possible program actions that can be done with the file: like open android file, edit android file, convert android file, view android file, play android file etc. (if exist software for corresponding action in's database).

Get File Extension And MIME Type In Android Development , This method will check for the dot '.' occurrence in the given filename. If it exists, then it will find the last position of the dot '.' and return the  How to get file name from file path in android. Ask Question @mostvenerablesir Please check this link for which can get you the Base name of your file and the

How to Get the File Extension of a File in Java, How to get file extension using java code. To get extension View; import android.widget.Button; import android.widget.TextView; public class  Thanks..but one doubt..If i want to search one drive fully means for example “J:” what i have to use in FILE_DIR. I tried with “J:” but it only lists the outer files not searching the folders in J drive..pls help

How to get file extension using java code, LOG) which are all just text files. But I just can't view them on my Android phone because it says that there are no programs assigned to that file type. Dropbox  This example shows how you can get file extensions and their MIME type in android. Algorithm: 1.) Create a new project by File-> New -> Android Project name it GetFileExtensionWithMIMEType. 2.) Write following permissions into android manifest.xml: <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE

  • The point I am trying to get across is to not use regular expression for a simple task like this, not error checking.
  • What if I browse a file from the Gallery of my android phone, where it returns the file name as "content://media/external/images/media/3669". Can I get file name extension in that? @MikeLentini
  • Can you explain the +1 part?
  • @RoCK Without the +1, it'd be, for example, ".jpg" instead of "jpg". Note the dot.
  • Can be simplified as fileName.substring(fileName.lastIndexOf(".") + 1).
  • as File#toURL() is deprecated this should be myfile.toURI().toURL().toString()
  • Case sensitive extensions? What about files called readme.TXT etc
  • String ext = "", fileName = file.getName().toLowerCase(); try { ext = MimeTypeMap.getFileExtensionFromUrl(fileName); } catch (Exception e) { e.printStackTrace(); } worked for me.
  • @zanky That does work a lot of the time, but there are cases (I forget which ones exactly -- it's been several month) where it fails unexpectedly. In my experience, calling Uri.encode on the file name drove the failure case down to zero.
  • @Xavi...ya, you might be right but in my case when i used Uri.Encode it was returning encodes extension of file which was having format something like this 3gp%4a%5D... so i removed encode() method.
  • @zanky Huh, that's really weird. I'll keep an eye out for that. Thanks for letting me know.
  • What do you mean by "Stuff is"?