0

This interface provides one method, namely getContentTypeFor(String fileName). However, I have no idea how to use it. I implemented interface in Eclipse and ended with:

import java.net.FileNameMap;

public class Fnam implements FileNameMap {

public static void main(String[] args) {

}

@Override
public String getContentTypeFor(String fileName) {
    return null;
}
}

The method returns null. How should I change it to get the MIME type ?

kjdkfjsdo8
  • 97
  • 12
  • How is it a duplicate ? My question refers to usage of FileNameMap interface, not getting a Mime type of a file in general – kjdkfjsdo8 Jan 02 '17 at 08:55

2 Answers2

0

A FileNameMap is an interface returned by methods of a class that understands filetypes. For example there is a URLConnection class that has a getFileNameMap() method, which is used like this.

private void requestIntent(Uri uri) {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    FileNameMap mime = URLConnection.getFileNameMap();
    String mimeType = mime.getContentTypeFor(uri.getPath());
    intent.setDataAndType(uri, mimeType);
    try {
        mActivity.startActivity(intent);
    } catch (ActivityNotFoundException e) {
        Toast.makeText(mActivity, OResource.string(mActivity, R.string.toast_no_activity_found_to_handle_file), Toast.LENGTH_LONG).show();
    }
}

That example from here

So, you usually do not use FileNameMap in isolation. Instead you either use an existing library class that creates objects that implement FileNameMap. If you did want to implement such a library you would need to write code like this (taken from the source of UrlConnection)

public static synchronized FileNameMap More getFileNameMap() {
316         if ((fileNameMap == null) && !fileNameMapLoaded) {
317             fileNameMap = sun.net.www.MimeTable.loadTable();
318             fileNameMapLoaded = true;
319         }
320 
321         return new FileNameMap() {
322             private FileNameMap map = fileNameMap;
323             public String getContentTypeFor(String fileName) {
324                 return map.getContentTypeFor(fileName);
325             }
326         };
327     }

You see here that the implementation creates an anonymous class implementing the interface; your responsibility as implementor of the interface would be to figure a way to implement that getContentTypeFor() method.

If all you want to do is get the mime type for a file then you can use URLConnection to give you an object that already has that implementation, and so just use the approach shown in the answer to the related question

Community
  • 1
  • 1
djna
  • 54,992
  • 14
  • 74
  • 117
  • The first example refers to Android and not plain Java, the second has some problems when I tried to run it – kjdkfjsdo8 Jan 02 '17 at 11:57
0

It is interface for inner implementation of JDK, and in most cases you should use just interface implementation, not interface itself.

Here simple sample of usage:

public class Main {
    private static FileNameMap fileNameMap = URLConnection.getFileNameMap();

    public static void main(String... str) {
        System.out.print(fileNameMap.getContentTypeFor("my_file.xml"));
    }

}

prints:

application/xml

And seems only MimeTable is only current implementation of that interface in JDK.

Divers
  • 9,531
  • 7
  • 45
  • 88