Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import android.content.Context;

import android.net.Uri;

import android.text.TextUtils;

import android.webkit.MimeTypeMap;

public class Main {
    /**
     * Return the MIME type from the URI
     * @param context Context
     * @param uri URI
     * @return Return the MIME type
     */
    public static String getMimetypeFromUri(Context context, Uri uri) {
        String contentType = context.getContentResolver().getType(uri);
        if (TextUtils.isEmpty(contentType)) {
            final MimeTypeMap type_map = MimeTypeMap.getSingleton();
            // Get the extension from the path
            String extension = MimeTypeMap.getFileExtensionFromUrl(uri.toString());
            extension = extension.toLowerCase();
            if (extension.contains(".")) {
                extension = extension.substring(extension.lastIndexOf("."));
            }
            contentType = type_map.getMimeTypeFromExtension(extension);
        }
        return contentType;
    }
}