Android examples for Media:Video
Get the video thumbnail
/* /* w w w. j ava2s . com*/ * Copyright (C) 2007-2008 OpenIntents.org * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ //package com.book2s; import android.content.Context; import android.database.Cursor; import android.graphics.Bitmap; import android.provider.MediaStore; public class Main { /** * Get the video thumbnail * @param context * @param path * @param width * @param height * @return Bitmap */ public static Bitmap getVideoThumbnail(Context context, String path, int width, int height) { Cursor fileCursor = MediaStore.Images.Media.query( context.getContentResolver(), MediaStore.Video.Media.EXTERNAL_CONTENT_URI, null, MediaStore.Video.Media.DATA + " = '" + path + "'", null); int fileId = -1; if (fileCursor != null) { if (fileCursor.moveToFirst()) { int column = fileCursor .getColumnIndex(MediaStore.Video.Media._ID); if (column != -1) { fileId = fileCursor.getInt(column); } } fileCursor.close(); } if (fileId == -1) { path = path.substring(path.lastIndexOf("/") + 1); fileCursor = MediaStore.Images.Media .query(context.getContentResolver(), MediaStore.Video.Media.EXTERNAL_CONTENT_URI, null, MediaStore.Video.Media.DATA + " LIKE '%" + path + "%'", null); if (fileCursor != null) { if (fileCursor.moveToFirst()) { int column = fileCursor .getColumnIndex(MediaStore.Video.Media._ID); if (column != -1) { fileId = fileCursor.getInt(column); } } fileCursor.close(); } } if (fileId != -1) { String[] whereArgs = new String[1]; whereArgs[0] = String.valueOf(fileId); Cursor cursor = context.getContentResolver().query( MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI, null, MediaStore.Video.Thumbnails._ID + " = ?" + fileId, whereArgs, null); if (cursor != null) { if (cursor.moveToFirst()) { int column = cursor .getColumnIndex(MediaStore.Images.Thumbnails._ID); int thumbnailId = cursor.getInt(column); Bitmap bitmap = MediaStore.Video.Thumbnails .getThumbnail(context.getContentResolver(), thumbnailId, MediaStore.Video.Thumbnails.MICRO_KIND, null); if (bitmap != null) { bitmap = Bitmap.createScaledBitmap(bitmap, width, height, false); if (bitmap != null) { cursor.close(); return bitmap; } } } else { // thumbnail not found - generate it Bitmap bitmap = MediaStore.Video.Thumbnails .getThumbnail(context.getContentResolver(), fileId, MediaStore.Video.Thumbnails.MICRO_KIND, null); if (bitmap != null) { bitmap = Bitmap.createScaledBitmap(bitmap, width, height, false); if (bitmap != null) { cursor.close(); return bitmap; } } } cursor.close(); } } return null; } }