MimeTypeUtil.java revision b82d80d891077ccd74729e4143925a66eb89eef2
1b5e65c8bd3785409d4aeda21f2c88e75c9e22c9fSunny Goyal/*
2b5e65c8bd3785409d4aeda21f2c88e75c9e22c9fSunny Goyal * Copyright (C) 2014 The Android Open Source Project
3b5e65c8bd3785409d4aeda21f2c88e75c9e22c9fSunny Goyal *
4b5e65c8bd3785409d4aeda21f2c88e75c9e22c9fSunny Goyal * Licensed under the Apache License, Version 2.0 (the "License");
5b5e65c8bd3785409d4aeda21f2c88e75c9e22c9fSunny Goyal * you may not use this file except in compliance with the License.
6b5e65c8bd3785409d4aeda21f2c88e75c9e22c9fSunny Goyal * You may obtain a copy of the License at
7b5e65c8bd3785409d4aeda21f2c88e75c9e22c9fSunny Goyal *
8b5e65c8bd3785409d4aeda21f2c88e75c9e22c9fSunny Goyal *      http://www.apache.org/licenses/LICENSE-2.0
9b5e65c8bd3785409d4aeda21f2c88e75c9e22c9fSunny Goyal *
10b5e65c8bd3785409d4aeda21f2c88e75c9e22c9fSunny Goyal * Unless required by applicable law or agreed to in writing, software
11b5e65c8bd3785409d4aeda21f2c88e75c9e22c9fSunny Goyal * distributed under the License is distributed on an "AS IS" BASIS,
12b5e65c8bd3785409d4aeda21f2c88e75c9e22c9fSunny Goyal * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13b5e65c8bd3785409d4aeda21f2c88e75c9e22c9fSunny Goyal * See the License for the specific language governing permissions and
14b5e65c8bd3785409d4aeda21f2c88e75c9e22c9fSunny Goyal * limitations under the License.
15b5e65c8bd3785409d4aeda21f2c88e75c9e22c9fSunny Goyal */
16b5e65c8bd3785409d4aeda21f2c88e75c9e22c9fSunny Goyal
17b5e65c8bd3785409d4aeda21f2c88e75c9e22c9fSunny Goyalpackage com.android.nfc.beam;
18b5e65c8bd3785409d4aeda21f2c88e75c9e22c9fSunny Goyal
19b5e65c8bd3785409d4aeda21f2c88e75c9e22c9fSunny Goyalimport android.content.ContentResolver;
20b5e65c8bd3785409d4aeda21f2c88e75c9e22c9fSunny Goyalimport android.content.Context;
217886c037f87ba2274e95b160d125b02bb3928449Rajeev Kumarimport android.net.Uri;
22b5e65c8bd3785409d4aeda21f2c88e75c9e22c9fSunny Goyalimport android.util.Log;
232ae9c85bd764b92718e96ac95d73b1fd26c0e74eSunny Goyalimport android.webkit.MimeTypeMap;
242ae9c85bd764b92718e96ac95d73b1fd26c0e74eSunny Goyal
25b5e65c8bd3785409d4aeda21f2c88e75c9e22c9fSunny Goyalpublic final class MimeTypeUtil {
26b5e65c8bd3785409d4aeda21f2c88e75c9e22c9fSunny Goyal
27b5e65c8bd3785409d4aeda21f2c88e75c9e22c9fSunny Goyal    private static final String TAG = "MimeTypeUtil";
28b5e65c8bd3785409d4aeda21f2c88e75c9e22c9fSunny Goyal
29b5e65c8bd3785409d4aeda21f2c88e75c9e22c9fSunny Goyal    private MimeTypeUtil() {}
30b5e65c8bd3785409d4aeda21f2c88e75c9e22c9fSunny Goyal
317886c037f87ba2274e95b160d125b02bb3928449Rajeev Kumar    public static String getMimeTypeForUri(Context context, Uri uri) {
329e76f682f3e52afa1f11172564b883c7dfda5063Sunny Goyal        if (uri.getScheme() == null) return null;
339e76f682f3e52afa1f11172564b883c7dfda5063Sunny Goyal
347886c037f87ba2274e95b160d125b02bb3928449Rajeev Kumar        if (uri.getScheme().equals(ContentResolver.SCHEME_CONTENT)) {
359e76f682f3e52afa1f11172564b883c7dfda5063Sunny Goyal            ContentResolver cr = context.getContentResolver();
369e76f682f3e52afa1f11172564b883c7dfda5063Sunny Goyal            return cr.getType(uri);
379e76f682f3e52afa1f11172564b883c7dfda5063Sunny Goyal        } else if (uri.getScheme().equals(ContentResolver.SCHEME_FILE)) {
387886c037f87ba2274e95b160d125b02bb3928449Rajeev Kumar            String extension = MimeTypeMap.getFileExtensionFromUrl(uri.getPath()).toLowerCase();
399e76f682f3e52afa1f11172564b883c7dfda5063Sunny Goyal            if (extension != null) {
409e76f682f3e52afa1f11172564b883c7dfda5063Sunny Goyal                return MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
41b5e65c8bd3785409d4aeda21f2c88e75c9e22c9fSunny Goyal            } else {
42b5e65c8bd3785409d4aeda21f2c88e75c9e22c9fSunny Goyal                return null;
432ae9c85bd764b92718e96ac95d73b1fd26c0e74eSunny Goyal            }
44b5e65c8bd3785409d4aeda21f2c88e75c9e22c9fSunny Goyal        } else {
45b5e65c8bd3785409d4aeda21f2c88e75c9e22c9fSunny Goyal            Log.d(TAG, "Could not determine mime type for Uri " + uri);
46b5e65c8bd3785409d4aeda21f2c88e75c9e22c9fSunny Goyal            return null;
47b5e65c8bd3785409d4aeda21f2c88e75c9e22c9fSunny Goyal        }
48b5e65c8bd3785409d4aeda21f2c88e75c9e22c9fSunny Goyal    }
492ae9c85bd764b92718e96ac95d73b1fd26c0e74eSunny Goyal}
502ae9c85bd764b92718e96ac95d73b1fd26c0e74eSunny Goyal