Android examples for android.media:Video Thumbnail
get Thumbnail for image, video
import android.annotation.SuppressLint; import android.content.ContentResolver; import android.content.ContentUris; import android.content.Context; import android.content.Intent; import android.database.Cursor; import android.database.DatabaseUtils; import android.graphics.Bitmap; import android.net.Uri; import android.os.Build; import android.os.Environment; import android.provider.DocumentsContract; import android.provider.MediaStore; import android.util.Log; import android.webkit.MimeTypeMap; import java.io.File; import java.io.FileFilter; import java.text.DecimalFormat; import java.util.Comparator; public class Main{ /**/* w w w . j a v a 2 s .c o m*/ * Attempt to retrieve the thumbnail of given File from the MediaStore. This * should not be called on the UI thread. * * @param context * @param file * @return * @author paulburke */ public static Bitmap getThumbnail(Context context, File file) { return getThumbnail(context, getUri(file), getMimeType(file)); } /** * Attempt to retrieve the thumbnail of given Uri from the MediaStore. This * should not be called on the UI thread. * * @param context * @param uri * @return * @author paulburke */ public static Bitmap getThumbnail(Context context, Uri uri) { return getThumbnail(context, uri, getMimeType(context, uri)); } /** * Attempt to retrieve the thumbnail of given Uri from the MediaStore. This * should not be called on the UI thread. * * @param context * @param uri * @param mimeType * @return * @author paulburke */ public static Bitmap getThumbnail(Context context, Uri uri, String mimeType) { if (DEBUG) Log.d(TAG, "Attempting to get thumbnail"); if (!isMediaUri(uri)) { Log.e(TAG, "You can only retrieve thumbnails for images and videos."); return null; } Bitmap bm = null; if (uri != null) { final ContentResolver resolver = context.getContentResolver(); Cursor cursor = null; try { cursor = resolver.query(uri, null, null, null, null); if (cursor.moveToFirst()) { final int id = cursor.getInt(0); if (DEBUG) Log.d(TAG, "Got thumb ID: " + id); if (mimeType.contains("video")) { bm = MediaStore.Video.Thumbnails .getThumbnail( resolver, id, MediaStore.Video.Thumbnails.MINI_KIND, null); } else if (mimeType.contains(FileUtils.MIME_TYPE_IMAGE)) { bm = MediaStore.Images.Thumbnails.getThumbnail( resolver, id, MediaStore.Images.Thumbnails.MINI_KIND, null); } } } catch (Exception e) { if (DEBUG) Log.e(TAG, "getThumbnail", e); } finally { if (cursor != null) cursor.close(); } } return bm; } }