2

I am trying to get a thumbnail of a video. But unfortunately i am not able to get thumbnails.

        itmap bmThumbnail;

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

5 Answers5

2

I create video thumbnail like this in my app

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

 }
Chirag Savsani
  • 6,020
  • 4
  • 38
  • 74
  • I have tried it, but it didn't work for the videos that are on SD Card (External Storage) on Android 5.0+. It returned null bitmap – Dante May 23 '16 at 14:20
  • If you get video from sd card, then check first, Is video is in external storage or in some cloud storage. Because if video is on cloud storage, you will receive bitmap null. – Chirag Savsani May 24 '16 at 04:06
  • It has nothing to do with cloud. It is the new Storage Access Framework that screws it up. This won't work with SD card from Android 5.0 – Dante May 24 '16 at 11:10
1

In 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>

In 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);
Venkatesh Selvam
  • 1,382
  • 2
  • 15
  • 28
0

Try this

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

and for more details see this ThumbnailUtils

Zubair Akber
  • 2,760
  • 13
  • 30
0

It's not too hard to do. Something like this:

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);

See more about MediaStore at: http://developer.android.com/reference/android/provider/MediaStore.Video.html

xxx
  • 3,315
  • 5
  • 21
  • 40
0

The following code runs perfectly:

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

If you do not go through CURSOR or If you have only PATH or FILE OBJECTS, you can use :

 public static Bitmap createVideoThumbnail (String filePath, int kind)

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

KishuDroid
  • 5,411
  • 4
  • 30
  • 47