Retrieve a thumbnail of a video

空扰寡人 提交于 2019-12-13 02:33:47

问题


I'm trying to make an application for showing the best movie trailers. I'd like to show a grid view with the thumbnails of each video and then clicking them open a new Activity for playing the video.

Given a moment of the video, how can I retrieve the thumbnail? If that is complicated the first frame is enought too. Thanks


回答1:


If you are using API 2.0 or newer this will work.

To get video id:

String[] proj = {
    MediaStore.Video.Media._ID,
        MediaStore.Video.Media.DISPLAY_NAME,
    MediaStore.Video.Media.DATA
};

Cursor cursor = managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, 
                                    proj, MediaStore.Video.Media.DISPLAY_NAME+"=?",new String[] {"name.mp4"}, null);
cursor.moveToFirst()
id = cursor.getLong(cursor.getColumnIndex(MediaStore.Video.Media._ID));

To get the thumbnail of the video:

ImageView iv = (ImageView ) convertView.findViewById(R.id.imagePreview);
ContentResolver crThumb = getContentResolver();
BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize = 1;
Bitmap curThumb = MediaStore.Video.Thumbnails.getThumbnail(crThumb, id, MediaStore.Video.Thumbnails.MICRO_KIND, options);
iv.setImageBitmap(curThumb);

EDIT:

If you are on android-8 (Froyo) or above, you can use ThumbnailUtils.createVideoThumbnail from video path:

Bitmap thumb = ThumbnailUtils.createVideoThumbnail(path,
    MediaStore.Images.Thumbnails.MINI_KIND);

Hope it helps!



来源:https://stackoverflow.com/questions/7540798/retrieve-a-thumbnail-of-a-video

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!