1

I want to know whether the file is Audio(mp3,aac, etc), Video(mp4,wmv,3gp,etc), Document(txt,rtf,doc,docx,xls,xlsx,ppt,html etc) or Unknown (with no extension and other custom extension specific for one single application in the near future)

I need to implement it in Android. I think I could find the extension of file and identifying its type? Or do I not need to know the extension?

I just want to know the file-type.

Vogel612
  • 5,620
  • 5
  • 48
  • 73
Ahamed
  • 39,245
  • 13
  • 40
  • 68

5 Answers5

1
import java.net.FileNameMap;
import java.net.URLConnection;

public class FileUtils {

public static String getMimeType(String fileUrl)
  throws java.io.IOException {
      FileNameMap fileNameMap = URLConnection.getFileNameMap();
      String type = fileNameMap.getContentTypeFor(fileUrl);

     return type;
}

public static void main(String args[]) throws Exception {
      System.out.println(FileUtils.getMimeType("file://c:/temp/test.TXT"));
     // output :  text/plain
   }
}

http://www.rgagnon.com/javadetails/java-0487.html

Prasad S Deshpande
  • 1,362
  • 4
  • 15
  • 35
0

on Android, you can try something like this:

File f = <YOUR FILE>

String path = f.getAbsolutePath();
String fileExtension = null;
int dotPos = path.lastIndexOf('.');
if (0 <= dotPos) {
    fileExtension = path.substring(dotPos + 1);
}

if (!TextUtils.isEmpty(fileExtension )) {
    String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileExtension.toLowerCase());
    if (!TextUtils.isEmpty(mime)) {
        if (mime.startsWith("text")) {
        // TEXT FILE
        } else if (mime.startsWith("image")) {
        // IMAGE FILE
        }
    }
}
Ondřej Z
  • 5,094
  • 3
  • 24
  • 30
0

You can either try to guess the file type from the filename extension using guessContentTypeFromName(), or you can try to guess the file type from the content using guessContentTypeFromStream().

UgglyNoodle
  • 3,017
  • 1
  • 21
  • 17
  • 2
    If you look at the source of `URLConnection.guessContentTypeFromStream()` you'll see it only knows about HTML, XML, zip files, and GIF images. It can't identify most of the file types the question asks for. `guessContentTypeFromName()` has a larger database, but the questioner specifically asked to identify files with no extension. – Dan Hulme Apr 22 '13 at 12:43
0

You can use mime-util library. My sample code will get the mime type of the file.

    String imagePath = "/tmp/test.txt";
    File file = new File(imagePath);

    if (MimeUtil.getMimeDetector(MagicMimeMimeDetector.class.getName()) == null)  {
        MimeUtil.registerMimeDetector(MagicMimeMimeDetector.class.getName());
    }
    Collection<MimeType> mimeTypes = MimeUtil.getMimeTypes(file);
    for (MimeType mimeType : mimeTypes) {
        System.out.println(mimeType.getMediaType());
    }
0

The shortest way to find out file extension is using lastIndexOf('.') assuming file name contains extension. Check the following code

String fileName = "test.jpg";
int i = fileName.lastIndexOf('.');
String fileExtension = fileName.substring(i+1);
Log.v("FILE EXTENSION: ",fileExtension);
Umesh
  • 1,609
  • 1
  • 17
  • 28