get File Extension - Android java.io

Android examples for java.io:File Name

Description

get File Extension

Demo Code

import android.annotation.SuppressLint;
import android.content.ClipDescription;
import android.content.Context;
import android.net.Uri;
import android.os.StatFs;
import android.util.Log;
import android.webkit.URLUtil;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class Main{

    /**//from   w w  w .  j ava2s .  c o  m
     * gets a file extension from the specified file 
     * @param file - the file to get the extension
     * @return
     */
    public static String getFileExtension(File file) {

        String fileName = file.getName();
        int pos = fileName.lastIndexOf(".");
        if (pos < 0) {
            return "";
        }

        String extension = fileName.substring(pos + 1);
        if (extension.length() > 5) {
            return "";
        }
        return extension.toLowerCase(Locale.US);

    }

}

Related Tutorials