1. There are two ways to get a video Thumbnail (1) through the content provider (2) create a thumbnail manually
(1) The disadvantage is that you must update the media library to see the thumbnail of the latest video.
/** * @param context * @param cr * @param Videopath * @return */ public static Bitmap getVideoThumbnail(Context context, ContentResolver cr, String Videopath) { ContentResolver testcr = context.getContentResolver(); String[] projection = { MediaStore.Video.Media.DATA, MediaStore.Video.Media._ID, }; String whereClause = MediaStore.Video.Media.DATA + " = '" + Videopath + "'"; Cursor cursor = testcr.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, projection, whereClause, null, null); int _id = 0; String videoPath = ""; if (cursor == null || cursor.getCount() == 0) { return null; } if (cursor.moveToFirst()) { int _idColumn = cursor.getColumnIndex(MediaStore.Video.Media._ID); int _dataColumn = cursor.getColumnIndex(MediaStore.Video.Media.DATA); do { _id = cursor.getInt(_idColumn); videoPath = cursor.getString(_dataColumn); } while (cursor.moveToNext()); } cursor.close(); BitmapFactory.Options options = new BitmapFactory.Options(); options.inDither = false; options.inPreferredConfig = Bitmap.Config.RGB_565; Bitmap bitmap = MediaStore.Video.Thumbnails.getThumbnail(cr, _id, Images.Thumbnails.MINI_KIND, options); return bitmap; }
(2) It takes a little time to create a thumbnail manually.
/*** Get the video thumbnail * @ Param videopath * @ Param width * @ Param height * @ Param kind * @ return */private bitmap getvideothumbnail (string videopath, int width, int height, int kind) {Bitmap bitmap = NULL; bitmap = thumbnailutils. createvideothumbnail (videopath, kind); bitmap = thumbnailutils. extractthumbnail (bitmap, width, height, thumbnailutils. options_recycle_input); Return bitmap ;}
2. Image thumbnails
/** * * @param context * @param cr * @param Imagepath * @return */ public static Bitmap getImageThumbnail(Context context, ContentResolver cr, String Imagepath) { ContentResolver testcr = context.getContentResolver(); String[] projection = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID, }; String whereClause = MediaStore.Images.Media.DATA + " = '" + Imagepath + "'"; Cursor cursor = testcr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, whereClause, null, null); int _id = 0; String imagePath = ""; if (cursor == null || cursor.getCount() == 0) { return null; } if (cursor.moveToFirst()) { int _idColumn = cursor.getColumnIndex(MediaStore.Images.Media._ID); int _dataColumn = cursor.getColumnIndex(MediaStore.Images.Media.DATA); do { _id = cursor.getInt(_idColumn); imagePath = cursor.getString(_dataColumn); } while (cursor.moveToNext()); } cursor.close(); BitmapFactory.Options options = new BitmapFactory.Options(); options.inDither = false; options.inPreferredConfig = Bitmap.Config.RGB_565; Bitmap bitmap = MediaStore.Images.Thumbnails.getThumbnail(cr, _id, Images.Thumbnails.MINI_KIND, options); return bitmap; }