get MediaStore Image Mime Type - Android Media

Android examples for Media:Picture

Description

get MediaStore Image Mime Type

Demo Code


//package com.java2s;

import android.content.Context;
import android.database.Cursor;

import android.net.Uri;

import android.provider.MediaStore.Images;
import android.provider.MediaStore.MediaColumns;

public class Main {
    private static final String[] MIME_TYPE_COLUMN = { MediaColumns.MIME_TYPE };

    public static String getImageMimeType(final Context aContext,
            final Uri uri) {

        Cursor cursor = null;/*from w ww . j  av  a 2  s. c  o m*/
        try {
            cursor = Images.Media.query(aContext.getContentResolver(), uri,
                    MIME_TYPE_COLUMN);

            if (null == cursor) {
                return null;
            }

            final int columnIndex = cursor
                    .getColumnIndex(MediaColumns.MIME_TYPE);
            if (-1 == columnIndex) {
                return null;
            }

            if (!cursor.moveToFirst()) {
                return null;
            }

            final String mimeType = cursor.getString(columnIndex);
            return mimeType;

        } catch (final NullPointerException e) {
            // This seems to get thrown internally If the image URI is no longer
            // in the DB.
            return null;
        } finally {
            if (null != cursor) {
                cursor.close();
            }
        }
    }
}

Related Tutorials