How do i get thumbnail of a video in android

和自甴很熟 提交于 2019-12-04 23:24:43

You can use glide. its automatically set thumb image of video.

Glide is also able to display the thumbnail of videos, as long as they're stored on the phone. Let's assume you get the file path by letting the user select a video: Based on this document https://futurestud.io/tutorials/glide-displaying-gifs-and-videos

String filePath = "/storage/emulated/0/Pictures/example_video.mp4";

Glide  
    .with(context)
    .asBitmap()
    .load(Uri.fromFile(new File(filePath)))
    .into(imageViewGifAsBitmap);

Here is the complete tested working solution:

Try it.

    if (videoFileUri != null) {

                        Bitmap bitmapThumb = null;
                        try {
                            bitmapThumb = ThumbnailUtils.createVideoThumbnail(videoFileUri.toString(),
                                    MediaStore.Video.Thumbnails.MINI_KIND);// MINI_KIND, size: 512 x 384 thumbnail | MICRO_KIND, size: 96 x 96 thumbnail

                            ImageView imageView = (ImageView) findViewById(R.id.image);
//                            imageView.setImageBitmap(bitmapThumb);

                            Uri uri = getImageUri(context, bitmapThumb);

                            Picasso.with(context)
                                    .load(uri)
                                    .placeholder(R.drawable.ic_imagedefault)
                                    .error(R.drawable.ic_imagedefault)
                                    .resize(350, 200)//as per need.
                                    .centerCrop()
                                    .into(imageView);

                        } catch (Exception e) {
                            e.printStackTrace();
                        } finally {
                            bitmapThumb = null;
                        }
                    }
                }

Here is the static method for getImageUri :

public static Uri getImageUri(Context inContext, Bitmap inImage) {

    try {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        inImage.compress(Bitmap.CompressFormat.PNG, 100, bytes);
        String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
        return Uri.parse(path);
    } catch (Exception e) {
        e.printStackTrace();
        return Uri.parse("");
    }

}
Karan Mahbubani
Uri uri = getImageUri(context, bitmapThumb);
Glide.with(getContext()).
load(uri).
thumbnail(0.1f).
into(imageView);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!