如何在Android中获取视频缩略图?

时间:2015-11-06 05:29:08

标签: android android-videoview video-thumbnails

我正在尝试获取视频的缩略图。但不幸的是我无法获得缩略图。

        itmap bmThumbnail;

        // MICRO_KIND: 96 x 96 thumbnail
        bmThumbnail = ThumbnailUtils.createVideoThumbnail("file://" + catBean.tempImage,
                MediaStore.Video.Thumbnails.MICRO_KIND);
        img_icon.setImageBitmap(bmThumbnail);

5 个答案:

答案 0 :(得分:2)

我在我的应用中创建了这样的视频缩略图

 Bitmap myBitmap = BitmapFactory.decodeFile(pathOfFile);
    if(myBitmap == null) {
        myBitmap = ThumbnailUtils.createVideoThumbnail(pathOfFile, Thumbnails.MICRO_KIND);

 }

答案 1 :(得分:1)

在Xml中:

 <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical" >

            <ImageView
                android:id="@+id/thumbnail"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical|center_horizontal" />
</LinearLayout>

在java中:

 long id = ""; //Video Id 

 ImageView thumbnail = (ImageView) findViewById(R.id.thumbnail);
 ContentResolver crThumb = getContentResolver();
 BitmapFactory.Options options = new BitmapFactory.Options();
 options.inSampleSize = 2;
 Bitmap curThumb = MediaStore.Video.Thumbnails.getThumbnail(crThumb, id,
                            MediaStore.Video.Thumbnails.MINI_KIND, options);
 thumbnail.setImageBitmap(curThumb);

答案 2 :(得分:0)

试试这个

Bitmap bMap = ThumbnailUtils.createVideoThumbnail(file.getAbsolutePath(), MediaStore.Video.Thumbnails.MICRO_KIND);

有关详细信息,请参阅此ThumbnailUtils

答案 3 :(得分:0)

这并不难做到。像这样:

int id = **"The Video's ID"**
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);

http://developer.android.com/reference/android/provider/MediaStore.Video.html

了解有关MediaStore的更多信息

答案 4 :(得分:0)

以下代码运行完美:

Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(file.getAbsolutePath(), MediaStore.Video.Thumbnails.MICRO_KIND);

如果您没有通过 CURSOR 或如果您只有路径或文件对象,则可以使用:

 public static Bitmap createVideoThumbnail (String filePath, int kind)

参考:http://developer.android.com/reference/android/media/ThumbnailUtils.html#createVideoThumbnail%28java.lang.String,%20int%29