Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/**
 * Appcelerator Titanium Mobile
 * Copyright (c) 2009-2010 by Appcelerator, Inc. All Rights Reserved.
 * Licensed under the terms of the Apache Public License
 * Please see the LICENSE included with this distribution for details.
 */

import java.util.HashMap;
import android.webkit.MimeTypeMap;

public class Main {
    public static final HashMap<String, String> EXTRA_MIMETYPES = new HashMap<String, String>();

    public static String getMimeType(String url) {
        return getMimeType(url, "application/octet-stream");
    }

    public static String getMimeType(String url, String defaultType) {
        String extension = "";
        int pos = url.lastIndexOf('.');
        if (pos > 0) {
            extension = url.substring(pos + 1);
        }
        return getMimeTypeFromFileExtension(extension, defaultType);
    }

    public static String getMimeTypeFromFileExtension(String extension, String defaultType) {
        MimeTypeMap mtm = MimeTypeMap.getSingleton();
        String mimetype = defaultType;

        if (extension != null) {
            String type = mtm.getMimeTypeFromExtension(extension);
            if (type != null) {
                mimetype = type;
            } else {
                String lowerExtension = extension.toLowerCase();
                if (EXTRA_MIMETYPES.containsKey(lowerExtension)) {
                    mimetype = EXTRA_MIMETYPES.get(lowerExtension);
                }
            }
        }

        return mimetype;
    }
}